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).