The Timer object can be a source of problems in actionscript 3 projects. In my recent project, I needed to do some script-based animation on a frame, and I used TweenLite (a great animation engine) to create a sequence of animations, and I needed the animation to repeat the sequence on and on until the user goes to another frame. I tried using the loop properties in the vars supplied to the TweenMax.from() function, but it does not do what I wanted it to. I don’t want the animation steps to repeat itself; I want the whole sequence of these small steps to repeat. For this purpose, I used the Timer object, and on each timer event, I call a function that will create those small animation steps in sequence. And with this, the animation on this particular frame works perfectly until I try to go to another frame.

On another frame, the Timer object continues to exist and fire the events, and the function triggered tried to animation objects that are no longer on the frame, and thus breaking the project. So I needed a way to remove or at least stop the timer object when execution moves onto another frame. The challenge is there is no event that can be listened to to know when execution moved off onto other frames. And I don’t want to use variables to keep references to Timer objects, which might end up preventing the Flash Player to garbage collect the timer instances.

In the end, I used a mechanism where a global array is used to keep track of data objects that keep a name and a reference to a Timer on any frame. When creating a timer on any frame, I add the timer-info object to the global array. I do not assign it to a particular variable just for that timer. To operate on the timer, I use a function call that will return the Timer object based on the timer name. And on entering any frame and before creating any timer, I first do a clean-up on potential timers that started their life from other frames – basically I loop through the global array, stop all of the timers the array knows about, and clear the array (setting the array to an empty array). This worked well for me. No script errors that are triggered by timers created on other frames. And the animations worked as expected.

Another potential alternative to solve the problem is to use the TweenGroup class that is bundled in the TweenLite and TweenMax packages. And, for the purpose of this post, I read the description for the TweenGroup, and it appears to be just the right code to achieve what I achieved using a method that I rolled myself. I would definitely give it a try the next time. I wish I had read about it earlier. I spent like an hour and a half to write up my own solution. But the TweenGroup is a much more versatile and powerful and easy-to-use solution. TweenGroup is another great reason to choose the TweenLite or TweenMax tweening engine over other animation engines.

  • Digg
  • del.icio.us
  • StumbleUpon
  • Sphinn
  • Facebook
  • Mixx
  • Google Bookmarks
  • Haohao
  • LinkedIn
  • Live
  • MyShare
  • MySpace
  • Reddit
  • Technorati
  • TwitThis

Related posts:

  1. Flash Tween Engines for AS3
  2. Bizarre Flash Behaviour
  3. 3D Flash Image Slider
  4. Making a Web-server-hosted Flash Project Playable from a Local Drive

Tags: , , , ,

Leave a Reply

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>