Timers in Actionscript 3

I'm writing this tutorial for someone who posted a comment on Basic Actionscript 3 Game Part 1 called sstng so if you would like to know how to do a certain thing in a language then feel free to post a comment and ask.

Timers in Actionscript 3 are really simple and easy to use and understand.

Sstng wanted to know how to create a timer that is triggered after 100 seconds.

Here is the code we would use to do this.


//set the time you want in milliseconds.
var myTimer:Timer = new Timer(100000, 1); //100 seconds
myTimer.addEventListener(TimerEvent.TIMER, theAction); //when the timer hits 100 seconds it will run the Action
myTimer.start() //start the timer

function theAction(event:TimerEvent) :void {
trace("Timer Triggered"); //output a message so you know its working
}

I hope this has helped.

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

, , , , , ,

5 Responses to Timers in Actionscript 3

  1. codebounce.com September 20, 2008 at 7:48 am #

    Thanks. Useful code. It does help :)

    Suggestion:
    Noticed you do not have a contact form in this site.
    I tried to contact you and this was the only way to let you know.

    Best
    Tom

  2. Flash Tutorials October 7, 2008 at 10:56 am #

    People mostly get confused in using action script but your tutorial shows that it is not to hard to understand.

  3. laura April 12, 2009 at 6:05 pm #

    hi, i have followed your code and put it into mine, im new at this and finding things hard to follow, the timer is not working on my code and i was wondering if someone could tell me what other code i need to put in and what i do to display this timer on the screen..

    here is my code so far

    stop();

    var score:Number=0;

    addEventListener(Event.ENTER_FRAME,testHit);
    function testHit(e:Event){
    if (kissBoy.hitTestObject(kissGirl)){
    trace("Hit");
    score+=5;
    scoreDisplay.text="score:"+score
    }
    }

    var myTimer:Timer=new Timer(60000,1);
    myTimer.addEventListener(TimerEvent.TIMER,gotoAndStop("gameWon")); //when the timer hits 60

    myTimer.start()

    function theAction(event:TimerEvent):void{
    trace("TimerTriggered");
    }
    function runalltime(evt:Event):void{
    if (kissBoy.hitTestPoint(kissGirl.x,kissGirl.y,true)){
    kissGirl.x=Math.random()*400;
    kissGirl.y=Math.random()*300;
    }

    }

    function keydetect(evt:KeyboardEvent):void{
    if(evt.keyCode==Keyboard.RIGHT){
    kissBoy.x+=10;
    }

    if(evt.keyCode==Keyboard.LEFT){
    kissBoy.x-=10;
    }

    if(evt.keyCode==Keyboard.UP){
    kissBoy.y-=10;
    }

    if(evt.keyCode==Keyboard.DOWN){
    kissBoy.y+=10;
    }
    }

    stage.addEventListener(Event.ENTER_FRAME,runalltime);
    stage.addEventListener(KeyboardEvent.KEY_DOWN,keydetect);

  4. Shervin Emami September 12, 2009 at 9:49 am #

    Nice blog. You might also want to mention that if someone wants the timer to keep running forever (as in they want it to call the function "theAction" every 100 seconds instead of just once after the first 100 seconds), then they can just leave the 2nd argument blank when they create the Timer.

    for example:

    var myTimer:Timer = new Timer(5000, 1); // call the function once, after 5 secs.
    var myTimer:Timer = new Timer(5000); // call the function repeatedly, every 5 secs.

Trackbacks/Pingbacks

  1. Recent Links Tagged With "actionscript3" - JabberTags - September 10, 2008

    [...] public links >> actionscript3 Timers in Actionscript 3 Saved by lroosma63 on Tue 09-9-2008 Blender 2.46のArmature(アーマチュア)... Saved by [...]

Leave a Reply