Blog
7

Suspended tween in Flash

If you have ever tried instantiating some tween during run time in flash, you might have noticed that sometimes your tween does not reach its endpoint. This can cause problem ranging from the category of a small nuisance, when you try to move something or make something fade out, to the halting of a complex process by breaking a chain of events when, you rely on the tween completion event to trigger another event in a long chain.

I first came across this suspended tween effect (a.k.a tween got stuck and tween not completing) in flash while working on a flash project that required me to move some entities around the screen upon receiving a mouse click event. To keep things easy to understand, let us say I had an array of objects named box and I had to move all items in box 100 units to right upon receiving a mouse click. I looped through the entire array and instantiated a tween for each of the elements in box.

for(i:int=0;i<box.length;i++) {

	var tween:Tween = new Tween(box[i], "x", Strong.easeOut, 0, 100, 1, true);

	tween.looping = false;
}

Soon I found that some of the boxes would not complete the movement from x=0 to x=100 and get stuck somewhere in between the starting and finishing position. Later, after googling for a while, I found the cause and a solution for this.

The suspended tween effect is caused by garbage collection of orphaned tween objects. If you don’t let your tween become orphans, they don’t get cleared by the garbage collector and your tweens get to reach their respective finishing points. A tween object becomes orphan as soon as it becomes out of scope. Notice that, in my previous code snippet, I have instantiated tween objects inside the loop. As soon as we are out of the loop, all the tween objects instantiated inside are out of scope and become orphans. When the garbage collector finds these orphan tween objects, they are cleared immediately and you see that your tweens have just suspended instead of reaching completion.

The solution is to make sure that your tween objects are not orphaned or out of scope before completion. You can ensure this by keeping a valid reference for the tween object for as long as it is not completed. In my case, I used an array of tween objects declared outside of the loop to hold the tween objects and all was fine.

tweens:Array = new Array();

for(i:int=0;i<box.length;i++) {

	var tween:Tween = new Tween(box[i], "x", Strong.easeOut, 0, 100, 1, true);

	tween.looping = false;

	tweens.push(tween);
}

7 Comments to “Suspended tween in Flash”

  • Thank you Raiyan, for finding reason of the problem, for the beautiful solution and of course for sharing it!!!! You’re a life savior! ;)

  • Just wanted to drop you a line to say, I enjoy reading your site. I thought about starting a blog myself but don’t have the time.
    Oh well maybe one day…. :)

  • Hello there! I came across your blog and was grateful, as I’ve recently pulled my hair out trying to figure out this tween issue. I’m trying to adapt your code so it affects the alpha of my one and only imageLoader. Below is my attempt (apologies in advance, but I’m a newbie!) Unfortunately, the image won’t even load. And when I change ‘Tween(imageLoader’ to ‘Tween(imageLoader[j]‘, like in your original solution, Flash says that ‘Property 0 isn’t found and there is no default value.’

    var tweenArray:Array = new Array();
    for (var j:int = 0; j < 2; j++)
    { var imageLoaderTween:Tween = new Tween(imageLoader, “alpha”, Regular.easeOut, 0, 1, 4, true);
    tweenArray.looping = false;
    tweenArray.push(imageLoaderTween);
    }

    Any advice you can offer would be appreciated !

  • Hello Gilbert Rios,
    Haven’t compiled your code yet, just telling you what I can find from a glance at it.

    The second line inside the loop is:

    tweenArray.looping = false;

    tweenArray is an array, it does not have the looping property. Let us remove this line and see what happens.

  • Thank You very much :D

  • Raiyan, many thanks for the email and posting. After reading your response, I had a ‘duh’ moment (smile). Removing the looping line did the trick. Much appreciated! /g

  • Mate, that is solid gold. Thank you for posting such an elegant and simple solution to a very, VERY annoying problem!

Post comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>