ActionScript 3 Timer Event vs Enter Frame Event

While playing the flash games, I always notice one thing.The thing is some objects need to move from one place to another place within a given time.

Suppose, You are playing a car racing game, and your aim is need to complete your lap within 5 seconds.But what will happen if your flash player will responding slow or more faster than defined frame rate based on your computer performance.

On good configuration PC you may complete your lap within time, But what about poor configuration PC ……? Yes, If car speed is control by Timer event.

TIMER Event:

A Timer object dispatches a TimerEvent objects whenever the Timer object reaches the interval specified by the Timer.delay property.

ENTER_FRAME Event:

The ENTER_FRAME event is dispatched on every frame.
See the below example:

In this example, You will find the difference between Timer event & ENTER_FRAME event.

Just click on GO button on the bottom left corner.

If you notice carefully, you will find the both jets are not flying with same speed. As per the code logic it is same
See the code here.
[as3]
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;

public class timevsframe extends MovieClip{

// Define timer variable & set delay to
private var timer:Timer = new Timer(40);

// Define timer for count the & set delay 40
// to check time every 40 miliseconds
private var countTime:Timer = new Timer(40);

// number of seconds
private var count:Number = 0;

// Enter Frame event called number of time
private var eF_evt_called:Number = 0;

// Timer event called number of time
private var t_evt_called:Number = 0

// Class constructor
public function timevsframe():void{
// Add events to the buttons
btnGo.addEventListener(MouseEvent.CLICK, start_race);

// Add event for count the time
countTime.addEventListener(TimerEvent.TIMER, startCounter);
}

// Start the race
private function start_race(e:MouseEvent):void{
// Add Enter Frame event for the red jet
this.addEventListener(Event.ENTER_FRAME, flyRed);
// Reset intial state
count = 0;
eF_evt_called = 0;
t_evt_called = 0;

jetRed.x = 100;
jetGreen.x = 100;

// Add Timer event for the green jet
timer.addEventListener(TimerEvent.TIMER, flyGreen);
// Start timer so timer will
// start the call events with specified delay
timer.start();
// Start time counter timer
countTime.start();
}

// Timer counter function
private function startCounter(e:TimerEvent):void{
count++;
var time:Number = Math.floor(count/25);
txtSec.text = String(time);

if(time >= 4){
stop_race();
}
}

private function stop_race():void{
timer.stop();
countTime.stop();
this.removeEventListener(Event.ENTER_FRAME, flyRed);
}

private function flyRed(e:Event):void{
jetRed.x = jetRed.x + 3;
eF_evt_called++;

// Printing number of ENTER_FRAME calls
txtFEvent.text = String(eF_evt_called);
}

private function flyGreen(e:TimerEvent):void{
t_evt_called++;
jetGreen.x = jetGreen.x + 3;

// Printing number of Timer calls
txtTEvent.text = String(t_evt_called);
}
}
}
[/as3]

Download Source [timevsframe.zip]

Lets describe you the code of this example:

Setting up Red Jet Fly:

Aim is jet needs to fly 300 pixels within 4 seconds & movie frame rate is set by 25 fps.
If we calculate the same, 300/4 = 75 pixels in 1 second, that means 75 pixels in 25 frames, how many pixels will fly the set within 1 frame.
Lets calculate 75 pixels / 25 frames = 3 pixels;
[as3]
private function flyRed(e:Event):void{
jetRed.x = jetRed.x + 3;
eF_evt_called++;

// Printing number of ENTER_FRAME calls
txtFEvent.text = String(eF_evt_called);
}
[/as3]

Setting up Green Jet Fly:

Same thing is here, the only difference is the function is called by timer event based on delay.
First set delay 40 so we can call the event 25 times within 1 second. 1 second = 1000 milliseconds. if we want to call event 25 time/sec then divide 1000/25 and then you will get 40 milliseconds.

So, delay will be 40 milliseconds.
[as3]
private var timer:Timer = new Timer(40);
[/as3]
Here delay is 40 milliseconds. That mean, the event calling 25 times within 1 second (1 second = 1000 millisecond). And I want to fly the jet 75 pixels in 1 seconds (event is calling 25 times within 1 second).
Calculate the pixel per event call: 75 pixels / 25 events = 3 pixels.
[as3]
private function flyGreen(e:TimerEvent):void{
t_evt_called++;
jetGreen.x = jetGreen.x + 3;

// Printing number of Timer calls
txtTEvent.text = String(t_evt_called);
}
[/as3]
If you have any comments, please write…

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *