Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » as » Basic Timer in AS3

Basic Timer in AS3

Learn how to create a basic Timer in ActionScript 3.

In this short tutorial we will explore the process of creating a timer, which will move an object when the timer expires.

Start by creating a new Flash file, add a movieclip to the stage and assign an instance name of objectMC.

Once the movieclip is created, the next step is to create the timer code.

var myTimer:Timer = new Timer(1000, 1);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);
myTimer.start();

That code creates a Timer instance, assigns an event handler and kicks off the timer countdown. The next step is to create the timer handler function.

function timerHandler(evt:TimerEvent):void
{
  objectMC.x = 200;
  objectMC.y = 200;
}

Now when you test the movie the object will sit on the stage and once the timer is complete the object will move.

What happens if you want to stop the timer? Well that is simple, just call the stop(); method of the Timer class.

myTimer.stop();

You have completed the code required for this example. Feel free to expand on the example and begin using timers in your own code.

Here is the complete code for this example:


// Requires a movieclip with the instance name "objectMC" to be on the stage

var myTimer:Timer = new Timer(1000, 1);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);
myTimer.start();

function timerHandler(evt:TimerEvent):void
{
  objectMC.x = 200;
  objectMC.y = 200;
}

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Basic Timer in AS3

 Abid Amin Khan  Thu Oct 21, 2010 6:11 am  
Thanks for spreading useful information. I have a question.
I am trying to load JPEG's (4 picture files) and i want the pic the loaded one after another after 5minuntes delay.
waiting for your king response.
 mkeefe  Fri Oct 29, 2010 10:45 pm  
@Abid Amin Khan - To load the graphics in a 5 minute interval simply create a 5 minute timer and loop through an array of images. Removing and loading one image every 5 minutes.
 GameCla.im  Mon Mar 7, 2011 11:32 am  
Thanks, I keep coming back for this code! Doesnt stick well in my head lol
 mkeefe  Mon Apr 4, 2011 5:32 pm  
@GameCla.im - That is actually the reason a lot of the tutorials are created on this site. That way we can all reference them, without having to dig through local code/notes. :)
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles