why do tweens stop randomly?

With code like the following, sometimes the child controls correctly finish their animation and sometimes they stop at random places in the middle. Why don't they work correctly?

var t:Tween;
t = new Tween(child1,"x",Elastic.easeOut,0,100,2,true);
t = new Tween(child1,"y", Elastic.easeOut,0,100,2,true);
t = new Tween(child2,"x",Strong.easeOut,300,400,1,true);
t = new Tween(child2,"y", Strong.easeOut,300,400,1,true);


You can also create an array in the scope of your class, then just push tweens onto that array. Although this might cause the tweens in the array to never get garbage collected, even after they finish, so you might want to empty the array yourself at points in which you know all the tweens have finished.

Additionally, try using an free/open source tween engine rather than the one packaged with Flash. Two very popular ones are TweenLite and Tweener. They offer greater performance and more functionality/options.

Each tween must be assigned to a separate variable in global scope. The following code behaves reliably:

var t1:Tween = new Tween(child1,"x",Elastic.easeOut,0,100,2,true);
var t2:Tween = new Tween(child1,"y", Elastic.easeOut,0,100,2,true);
var t3:Tween = new Tween(child2,"x",Strong.easeOut,300,400,1,true);
var t4:Tween = new Tween(child2,"y", Strong.easeOut,300,400,1,true);

It would appear that when a variable is re-used, the tween that is no longer referenced may get garbage collected or otherwise halted in the middle of its work.

The same problem can occur if you use separate variables but declare them in the local scope of a function instead of in the global scope of your frame.

Need Your Help

Unreferenced Array not so Unreferenced after "slice"?

javascript arrays pass-by-reference

Note: this isn't one of the millions dups of the common array copy 'problem' where using arr.slice(0) fixes this 'problem'

modifying windows 7 hosts file doesn't take effect

windows dns localhost virtualhost

I'm trying to define some fake domains to point to, but it does not take effect at all.