Flash: Timer, Frames and Animation

Flash has javascript like setTimeout() and setInterval() functions, but they are deprecated now in favor of "flash.utils.Timer" class.

Recap: setTimeout(callback, time_in_milliseconds) calls the callback after time_in_milliseconds milliseconds from now. setInterval(callback, time_in_milliseconds) calls callback repeatedly at time_in_milliseconds milliseconds interval. setInterval() returns a handle that can be passed to clearInterval() to stop the timer from firing. All these methods are accessible globally without importing anything.

flash.utils.Timer

Timer class constructor takes time_in_milliseconds as delay and repeat_count as the number of times the timer is to be fired.

Instead of callback based mechanism used by setTimeout() setInterval(), Timer class relies on flash's event handling feature. Timer class has an related class TimerEvent in the same package, it needs to be imported too.

Event handling in done in flash via subclassing flash.events.EventDispatcher, and flash.utils.Timer class is a subclass of EventDispatcher too. This means we have access to .addEventListener() method on Timer instances, that can be used to bind event handler with timer event.

Instances of Timer class has .start() and .stop() methods that can be used to start and stop the timers.

Here is an example usage:

package
{
    import flash.display.Sprite;
    import flash.utils.Timer;
    import flash.utils.TimerEvent;

    public class TimerExample extends Sprite
    {
        public function TimerExample()
        {
            var time_in_milliseconds:Number = 1000;
            var repeat_count:Number = 10;

            var timer:Timer = new Timer(time_in_milliseconds, repeat_count);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start();
        }

        public function onTimer(event:TimerEvent):void
        {
            trace("Timer called");
        }
    }
}

flash.events.Event.ENTER_FRAME

It so happens that flash has another mechanism for doing things periodically, and that is by using "frame event".

Flash application has a configuration option, FPS, frames per second, typically 60, but can be almost anything, of course it can not be very high. Flash updates the display at this frequency. This is when the frames of a playing video are updated, or when things move during an animation. Setting it low lowers the CPU requirements where as setting it high makes animation and playback smoother.

Throughout the application, an event "flash.events.Event.ENTER_FRAME" is available, this event is fired at the beginning of each frame. As you can see we do not have much control over the frequency of this event, but this event is synchronizes with everything else, so there are pros and cons of using this event vs the Timer discussed above.

It so happens that Sprite class also is a subsclass of EventDispatcher, so it .addEventListener() method. Here is a sample application using this method and ENTER_FRAME:

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    [SWF(width="760", height="570", frameRate="60")]
    public class EnterFrameExample extends Sprite
    {
        public function TimerExample()
        {
            this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        public function onEnterFrame(event:Event):void
        {
            trace("new frame");
        }
    }
}

Here we have explicitly set the frameRate to 60, so our onEnterFrame is called 60 times per second.

One of the main advantage of using Event.ENTER_FRAME over TimerEvent.TIMER is in animations, if we update the display at a higher frequency than it is being displayed it is wasted work, and it might cause unexpected drops in animation frames. If we are trying to be as smooth as possible, ENTER_FRAME event is preferable.

flash.utils.getTimer()

flash.utils.getTimer() is function that returns number of milliseconds since application started.

Timers and Frame Rates Are Approximate

A final note of warning, the frame rate and the time_in_milliseconds we pass to Timer are approximate values, flash does not give a gaurantee that the event handlers are called at exactly the specified times.

Various factors like CPU load, number of objects in displaylist, amount of work done in individual event handlers and so on control the actual rate at which our timers and frames are called.

Back to Flash For Python Developers.

 
0 Kudos
blog comments powered by Disqus