Bug in AS3 Tween Class

While I was conducting class a few days back, I did a demonstration on the Tween class and discovered a bug along the way.

The bug can be easily reproduced by using a Timer and creating a large number of Tweens on a large number of objects on the screen. What happens is that some of the tweens will proceed halfway and then hang there indefinitely. While others might continue tweening. The occurrence of the bug is completely random. If you have answer to this quirk/bug, please leave a comment below to let me know.

21 Responses to “Bug in AS3 Tween Class”


  • I have the same problem, and I can’t put any logics inside the finish event, cause there is a risk that the event will never fired.

  • Hey, maybe we should use this one:
    http://code.google.com/p/tweener/source

  • hello alvin! haha.. Got blog also never say.. boo~~ :D

    Anyway, what’s a use case of having a large number of Tweens on a large number of objects? The only use case I can think of is stressing your machine. :P

  • Yeah, irritating bug indeed.

    Seems to work if you declare each Tween instance you intend to use globally in your class file.

  • You might want to check out TweenLite at http://www.tweenlite.com for an alternate tweening engine. Very lightweight and fast.

  • Sounds like garbage collection eating up the Tween refs. I read somewhere that the original Tween class from AS2 keeps a static array of every tween created so that garbage collection doesn’t dispose of the object before the Tween is completed. Maybe AS3 tween doesn’t do this for some reason??

  • like rudestar said, the problem is with the garbage collector wich deletes reference to the Tween to early. with this knowledge i created a simple ‘TweenManager’ wich always has an internal reference to the Tween so it should not be picked up by the garbage collector. below is a quick made version, wich seems to work fine for me.

    // var tween = TweenManager.create(…)
    package
    {
    import fl.transitions.*;
    import fl.transitions.easing.*;

    final public class TweenManager
    {
    static private var __tweens:Array = new Array();

    static public function create($obj:Object, $prop:String, $func:Function, $begin:Number, $finish:Number, $duration:Number, $useSeconds:Boolean=true):Tween
    {
    var $tween:Tween = new Tween($obj, $prop, $func, $begin, $finish, $duration, $useSeconds);
    TweenManager.__tweens.push($tween);
    return $tween;
    }

    static public function remove($tween:Tween):void
    {
    for(var $k in TweenManager.__tweens)
    {
    if(TweenManager.__tweens[$k] === $tween)
    {
    $tween = null;
    TweenManager.__tweens[$k] = null;
    break;
    }
    }
    }
    }
    }

  • Or…just dont place too many tweens at one time.

    http://fiqo.blogspot.com

  • I have the same problem.

  • I’ve only seen this issue in Firefox / Safari and Chrome. Seems to not be a problem in IE (wow, IE wins for once).

    I don’t have multiple tweens occuring at once, but after one the next few may or may not work and then they will start working again.

    I don’t agree with the advice to not use too many tweens at once. I mean I could have been old school and set these things up on the timeline and they would have worked fine I’m sure. Adobe needs to fix the bug if they want people to migrate to object oriented AS3 development

  • roelof, how does one implement your package? I’m not familiar with using them. Thanks for the solution.

  • thank you thank you thank you thank you roelof!
    i work with as3 since 2 month and this bug bothered me since 1 month or something. now i’m ready to rebuild my love to flash. :)

  • Thank you so much!

    Your great idea has my latest app running nice and smoothly, now.

    Wish I could buy you a beer or ten.

  • Hi. I found this and is work for me. I got 21 tween in same time.

    http://www.scottgmorgan.com/blog/index.php/2007/11/18/as3-garbage-collection-the-reason-your-tweens-are-ending-early/

    Just use private var.

  • So I just ran into this problem also, and created a very quick, down and dirty, fix by storing the tweens in an array:

    var tweens:Array = new Array();

    function create_tween(){
    tweens[tween_count] = new Tween( whatever tween settings );
    tween_count++;
    }

    add an Motion_finish event listener so u can remove it when its done, or whatever else u want to do with it.

    Since the tween is references by the array, garbage collection wont be deleting it before its done its job.

  • Cam-

    Thanks for the solution! I was looking at alternative way to solve the Tween class in AS3.
    Much appreciation
    Frank

  • declaring the tweens globally worked for me. thanks!
    carl (::)

  • hi there,
    we got a simple solution for this kind of problem.

    We just pushed the new Tween object into an Array.

    example:
    myArray.push(new Tween(…params…))

    the tween is immedeatly executed when pushed into the array.

    greetings pensan & a fellow student

  • I’ve been having the same issue…using tweens for roll over and roll out mouse events and randomly a tween will freeze. I tried various methods global variables, housing the tweens in an array and also a Dictionary, and i even tried Tweenlite (which doesn’t work well with rollovers…even with overwrite manager). Still the issue persists…is there any real workaround? The thing is it is so inconsistant that is bugs me more…any help is appreciated. Thank you.

  • Yep. i’ve found two different bugs so far in CS4’s tweening.

    in one movie, AFTER publishing my swf file, the tweens randomly stop at different points within each run of that same file.

    in another movie, the swf file runs consistently, and the bug gets introduced BEFORE publishing the swf file. if i select and then deselect a shape (without performing any other action besides selecting and deselecting onscreen), then publish the file, it will stop at a different point than in the last swf file i exported.

    my conclusion: there is a bug in how flash cs4 exports .flash files with tweens to .swf files, AND in how both the FLASH cs4 and FLASHPLAYER cs4 applications handle .swf files with tweens. I haven’t checked these in a web browser just yet. hopefully the flash pug-in is less buggy.

    my work-around: I’ going to write my own tween class. Might publish the code on my website soon, for those interested.

  • Doing a bit of web surfing and observed your weblog appears to be like somewhat screwed up in my K-meleon web browser. However , fortunately hardly anyone makes use of it any more however you may want to look into it.

Leave a Reply