RSS link icon

Make a timer function in flash 

 

In this Flash actionscript 3.0 you will see how to create a timer function, how to set an animation to start at a specific interval.

 

 

 

The timer function is very useful, and is a must know if you want to do object oriented programming, and you can do very fun and creative stuff with it.

 

Basically a timer is build up by a eventlistener to "count" or listen and do a specific thing when the defined time is up. In our function I made a movieclip shapetween animation.

 

Now we need to make a movie clip animation, so make a shape and convert it to a movie clip. Remember to give your movieclip an instance name, I named mine "myAnimation".

 

Double click the movieclip and make a shapetween animation. (if you don't know how to do that, I think you should learn that before going on with actionscript, because thats basic flash).

 

Now double click the movieclip and open up actionscript on the first frame of the animation, type in stop(); this tells flash not to loop the animation again and again.

 

Go back to the main stage, open up the actionscript panel and your ready to type in some code.

 

Here is the code, I have made comments along the way in the code, to explain what every single code line does. (comments are the lines starting with // ).

 

 

// this first line create an instance of the timer object, and names it myTimer, also setting it to how long the timer shall wait until it starts, we set it to 5000, and thats milliseconds, so thats 5 seconds.

var myTimer:Timer = new Timer(5000);

 

// Now we add an eventlistener to our timer instance, this eventlistener "listens" to the timer and makes sure it counts the seconds and starts our function. (the function is also defined here, called doStuff).

myTimer.addEventListener(TimerEvent.TIMER, doStuff);

 

// Here we construct our function, this is the function here we tell flash what shall happen when the 5 seconds is gone, this is done within the { and in here we just tells our animation to start playing once }

function doStuff(event:TimerEvent):void {

myAnimation.play();

}

 

// The last and most important thing to do here is to start the timer, if we forget this, well nothing will happen, (I forget sometimes).

myTimer.start();

 

Well this was just a small simple example of a time, we can do a lot with the timer function, for example if we made a game, we can use a timer function to make enemies attack every 10 seconds. Or make a specific event happen every 30 seconds. Remember you can always have more then one timer instance (just name it something else).

 

 


Admin Bob says: Wednesday, September 03, 2008

Hi saenger, maybe its just me, but I think your description is a bit confusing, you may email me with your project and I may be able to assist you.

for the programming task, I would recommend you to learn a bit more programming, (if you dont know already).


saenger says: Saturday, August 30, 2008

Very interesting and helpful tutorial!!...
Im beginner and it would be nice if I could know how can I close the app with this function, for example, the user arent doing nothing and and a frame with a btn advices "do you need more time" or sth like that and:
if.. he push the btn the user continues in the app.
else...the app closes....
its very dificult can somebody help me
Thanks in Advance!!


Alin says: Sunday, May 11, 2008

This tutorial very helpful to me. Thanks.


Anonymous says: Wednesday, April 30, 2008

Thanks!!! This was EXACTLY what I was searching for.

   


 

 

 

 8

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact