thefoundationhttp://www.thefoundation.de2009-03-24T19:28:52Z(c) 2012 Michael Kurze, Aachen, GermanyHow to bring AS3 Tweens to an end2009-03-24T19:28:52ZDaniel Beckerhttp://www.thefoundation.de/about/danielhow-bring-as3-tweens-end<p>Have you ever assigned a tween to a local variable? Have you ever detected non-determining tweens, tweens which do not start at all? Blame ActionScript 3.0's ruthless garbage collector!</p><p>Back then in the old days of ActionScript 2.0 scripts like the following were just as easy as useful to program sequences of animations (<abbr title="exempli gratia">e. g.</abbr> assembling animations for site sections):</p> <code class="block"> function transitionToolTip():void { var toolTipAlpha:Tween = new Tween( toolTip, "alpha", Regular.easeOut, 0, 1, .75, true ); toolTipAlpha.addEventListener( TweenEvent.MOTION_FINISHED, handleToolTipTransition ); } function handleToolTipTransition( e:TweenEvent ):void { … } </code> <p class="annotation notice right">Just to make things clear: The ActionScript 3.0 garbage collector does a good job in deleting these local variables. Obviously the ActionScript 2.0 garbage collector should have done this way, too.</p><p>In most cases one does not want to stop or modify tweens after firing them, thus one won't need a reference to the exact tween ever again. The next function would be triggered after the tween has finished. Great!<br />But, nowadays things are different. ActionScript 3.0's garbage collector seriously and unrelentingly works on what it is supposed to work: deleting everything with a lack of relation. In the sample case, shown above, this clearly means that the variable <code>toolTipAlpha</code>, declared with local scope of function <code>transitionToolTip</code> will be deleted&nbsp;&ndash;&nbsp;taking along our tween if the garbage collection cycle is awkward. In consequence our tween will not play to its end.</p> <p>One solution would be the declaration of a class level or global variable to store the reference to the tween. This would look something like this:</p> <code class="block"> var toolTipAlpha:Tween; function transitionToolTip():void { toolTipAlpha = new Tween( toolTip, "alpha", Regular.easeOut, 0, 1, .75, true ); toolTipAlpha.addEventListener( TweenEvent.MOTION_FINISHED, handleToolTipTransition ); } function handleToolTipTransition( e:TweenEvent ):void { … } </code> <p class="annotation notice center">All the samples are in short form and not taken from real-life ActionScript 3.0 projects thus may not work properly. They are inserted for illustration purposes to show up possible problematics.</p> <p>A far more elaborated description of the coexistence of tweens, variables and the garbage collector can be found on <a title="Scott Morgan – LA Flash and Flex Developer" href="http://www.scottgmorgan.com/">Scott Morgan's</a> <a href="http://www.scottgmorgan.com/blog/">blog</a> in the article <a title="AS3 Garbage Collection, the reason your tweens are ending early." href="http://www.scottgmorgan.com/blog/index.php/2007/11/18/as3-garbage-collection-the-reason-your-tweens-are-ending-early/">AS3 Garbage Collection, the reason your tweens are ending early.</a></p> <p><a hreF="http://www.scottgmorgan.com/blog/index.php/2007/11/18/as3-garbage-collection-the-reason-your-tweens-are-ending-early/">via Scott Morgan's blog&nbsp;&ndash;&nbsp;http://www.scottgmorgan.com/</a></p><p class="annotation notice center">Sorry! Our Ping- and Trackback is not yet working.</p>