开发者

Tween does NOT repeat indefinitely

开发者 https://www.devze.com 2023-04-06 18:30 出处:网络
I am trying to animate a hexagon spinning like a wheel indefinitely: 开发者_如何学Cfunction rotateCW(e:TweenEvent = null):void{

I am trying to animate a hexagon spinning like a wheel indefinitely:

开发者_如何学Cfunction rotateCW(e:TweenEvent = null):void{
    var hexRot:Tween = new Tween(this, "rotationZ", None.easeNone, 0, 360, 2, true);
    hexRot.addEventListener(TweenEvent.MOTION_FINISH, rotateCW);        
}

For some strange reason the animation stops after a random amount of repetitions. It varies anything from between 2 and 600 times before it stops.

I've got a whole bunch of different events firing all over the place in my application, is it possible that this might cause the MOTION_FINISH event to either not fire or not get caught?

Thanks!


First of all you should define hexRot and listen to its MOTION_FINISH outisde of your function. By doing like you do, every hexRot stays in memory since it has a listener attached to it.

This may not solve your problem but it will be a cleaner way to write things and you will be less vulnerable to strange behaviors.

private var hexRot:Tween;

/**
*Run only once
*/
function init():void {
    hexRot = new Tween(this, "rotationZ", None.easeNone, 0, 360, 2, true);
    hexRot.addEventListener(TweenEvent.MOTION_FINISH, rotateCW); 
}

function rotateCW(e:TweenEvent = null):void{    
    hexRot.start();       
}


The flash Garbage collector has cleaned up your hexRot-variable, so the animation stopped. To fix this, use the solution Kodiak has provided :) When the variable is globally declared, it won't get collected as long as you don't put anything new in the variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消