Flash ActionScript Random Loop Animation Tutorial
For most loop flash animation, the motions are simply repeat again and again as shown in previous tutorial. Therefore this is a bit boring. In this flash actionscript tutorial, we show how to do a random loop animation. In other words, the position of the animation are changing all the time in a random manner.
Flash Tutorial Content:
In previous flash actionscript animation tutorial, the football is always enter and leave the stage at the same position. This kind of flash animation is a bit boring because the animation simply repeat again and agin. This would be more wonderful if the ball enter and leave the stage in a random position. In other words, the animation is not just repeat again and again, it is changing all the time. This flash actionscript tutorial will show how to use ENTER_FRAME event to do such random loop animation.
The complete Flash Movie is shown as above, you may try how it works before you start this tutorial. Click on the above flash movie to select it to start. Click on the Play button to turn on the animation. Click on the Stop button to stop the animation.
Flash ActionScript Codes:
//Function to obtain a random number between two numbers.
function getRandom(minNumber, maxNumber):Number {
tNumber = Math.round(Math.random()*(maxNumber - minNumber)) + minNumber;
return tNumber;
}
function moveFootball(evt:Event):void {
stage.addEventListener(Event.ENTER_FRAME, moveFootball);
//Set the frame rate
stage.frameRate = 15;
//Display location of the Footabll in the Message TextField.
output_txt.text = "Location of ball: " + football_mc.x + "," + football_mc.y
//Keep track the location of the ball
//If location small than the Stage Width, move to right by 1 pixel
//otherwise move back to the left of Stage by 10 pixel
//with random vertical position
if (football_mc.x < 512) {
football_mc.y = getRandom(60, 250);
}
//Call the moveFootball function when movie starts.
//Therefore the football will start moving when movie starts.
moveFootball(null);
function stopFootball(eve:Event):void {
}
//Button to stop the football
stop_btn.addEventListener(MouseEvent.CLICK, stopFootball);
//Button to move the football
play_btn.addEventListener(MouseEvent.CLICK, moveFootball);
Download Flash Source File:
Remarks:
This Flash ActionScript tutorial showed how to do a random loop animation. Some flash animations like to bounce an object back and forth (like ping pong). The next flash tutorial will show a very basic and simple bouncing animation.