Sequence Animation with Multiple Timers Flash ActionScript Tutorial
In fact, you can use more than one Timer in a Flash Movie depends on the complexity of your flash animation. We will use three Timers in this tutorial to do an interesting flash animation.
Flash Tutorial Content:
In this Flash ActionScript Timer Class animation tutorial, we do a more interesting flash animation with Timer Class. We use three Timers to do the flash animation in the Flash movie.
The complete Flash Movie of this tutorial is shown as above.
- the bird is looking down and up infinitely (Timer 1), and
- the bee is flying from the left to the right with random position (Timer 2),
- moreover the body of bee is also rotating (Timer 3).
Flash ActionScript Codes:
//Tween class lets you to move, resize, and fade movie clips
import fl.transitions.Tween;
//easing classes lets you do some elastic motion of movie clips
import fl.transitions.easing.*;
//import the TweenEvent class
//allow create a listener to catch the event when a tween finish.
import fl.transitions.TweenEvent;
//Declare and use later
var tweenRotationBody:Tween;
var tweenRotationTail:Tween;
var rotateDirection:String;
//Create a Timer object and store it in a variable called myClock.
//The timer will trigger every 3000 milliseconds (3 seconds)
//1000 milliseconds = 1 second
//3000 milliseconds = 3 seconds
//The repeatCount is zero, i.e. timer will trigger infinitely
//Summary:
//Create a Timer instance that will trigger every 3 seconds infinitely
var myClock:Timer = new Timer(3000, 0);
//Timer object created will not start automatically
//You have to start it
myClock.start();
//Add an event listener to the timer object (myClock)
//The event that listen is called TIMER which will be trigger every 3 seconds
//Every time the TIMER event is triggered, it will call the birdRotation function
myClock.addEventListener(TimerEvent.TIMER, birdRotation);
//Set the bird rotation direction
rotateDirection = "down";
//Function to rotate the bird's body and tail
//The rotation direction depends on the value of rotateDirection
function birdRotation(evt:TimerEvent):void {
rotateDirection = "up"
//Bird's body rotation DOWN tween animation
tweenRotationBody = new Tween(bird_mc, "rotation", Regular.easeOut, 0, 10, 2.5, true);
//Bird's Tail rotation UP tween animation
tweenRotationTail = new Tween(tail_mc, "rotation", Regular.easeOut, 0, 10, 2.5, true);
rotateDirection = "down"
//Bird's body rotation UP tween animation
tweenRotationBody = new Tween(bird_mc, "rotation", Regular.easeOut, 10, 0, 2.5, true);
//Bird's Tail rotation DOWN tween animation
tweenRotationTail = new Tween(tail_mc, "rotation", Regular.easeOut, 10, 0, 2.5, true);
}
//Function to obtain a random number between two numbers.
function getRandom(minNumber, maxNumber):Number{
tNumber = Math.round(Math.random()*(maxNumber - minNumber)) + minNumber;
return tNumber;
}
//Create a Timer instance that will trigger every 0.1 seconds infinitely
//This Timer is used to move the bee flying from left to right
var beeMove:Timer = new Timer(100, 0);
//Timer object created will not start automatically
//You have to start it
beeMove.start();
//Add an event listener to the timer object (beeClock)
//The event that listen is called TIMER which will be trigger every 7 seconds
//Every time the TIMER event is triggered, it will call the beeFly function
beeMove.addEventListener(TimerEvent.TIMER, beeFly);
function beeFly(evt:TimerEvent):void {
bee_mc.y += 0.5;
bee_mc.y = getRandom(50, 150);
}
//Create a Timer instance that will trigger every 8 seconds infinitely
//This Timer is used to move the bee flying from left to right
var beeRotate:Timer = new Timer(8000, 0);
//Timer object created will not start automatically
//You have to start it
beeRotate.start();
//Add an event listener to the timer object (beeClock)
//The event that listen is called TIMER which will be trigger every 7 seconds
//Every time the TIMER event is triggered, it will call the beeFly function
beeRotate.addEventListener(TimerEvent.TIMER, beeRotateBody);
var beeRotation:String = "rotateRight";
function beeRotateBody(evt:TimerEvent):void {
beeRotation = "rotateLeft";
beeRotation = "rotateRight";
}
Download Flash Source File:
Remarks:
This Flash ActionScript Timer Class tutorial showed how to do sequence animation with multiple timers.