Repeated Actions
At times in Flash CS3 and ActionScript you will want to perform an action continuously. You may need to see if a movie is playing or if an object has hit another object. Or, perhaps you want to move an object from piont a to b.
In Flash CS3 The Event.Enter_Frame is the line you will need. Event.ENTER_FRAME tells Flash CS3 to do this or that for every frame in the movie. This continuous action will happen even if the time line is stopped. It will happen as often as your frame rate is set for. Meaning if you run the movie at 30 frames per second then the Event.ENTER_FRAME will happen 30 times per second.
ENTER_FRAME
Once again in this Flash CS3 Tutorial some of the work is done for you. I have created an image of a car and the background of the street. I have already labeled the image of the car as car_mc. You just need to add the ActionScript 3.0. Select the first frame on the actions layer and create a new function using Event. Add the code from the example. Go ahead and press Control-Enter to see the movie in action.
| function drive (yourEvent:Event):void { |
| |
car_mc.x += 5 |
};
car_mc.addEventListener(Event.ENTER_FRAME, drive) |
If everything is correct the car will move down the street. In this Flash CS3 Tutorial, You set the function drive to run once on every key frame by using the Event.ENTER_FRAME method. You then added 5 pixels to the location of car_mc everytime the function was exicuted. Thus, making the car move down the street. You can speed the car up by speeding up the frame rate or by increasing the amount of pixels the car moves. You need to know that the function will continue to run even after the car has left the stage. Therefor, you need to end the function if there is no longer a need for it, or you are just wasting processing power. To remove an event listener us the removeEventListener method.
| function drive (yourEvent:Event):void { |
| |
car_mc.x += 5
if (car_mc.x >= 600){
car_mc.removeEventListener(Event.ENTER_FRAME, drive)
}
|
};
car_mc.addEventListener(Event.ENTER_FRAME, drive) |
In the example I added an if statement that checks to see if the car has movied
more then 600px. Since the stage is 700px, the car should stop at the edge of the stage. You can change the 600 to any number you want. I left it less then the stage width so you could actually see the car stop.
Timer
In Flash CS3 ENTER_FRAME is not the only way to coninually control an object. You can also create a timer. So,In the next part of this Flash CS3 Tutorial lets move the Plane with a timer. This code is alittle different in that you will need to create a variable. In Flash CS3 you create a varaible with the var tag. So in this case you are going to create the variable yourTimer. You are then going to Data Type it as a Timer, because it is part of proper coding. Next, you are going to set the constructors of the timer. The constructor takes two parameters, the first is a number in milliseconds and the second number is the number of intervals. You do not have to use the second parameter, but without it the timer will run until you stop it. That means, the timer you just created will be called every 10 miliseconds, and it will be called 170 times. 170 is just long enough to get the plane off the stage. You will want to increase it because the shadow is still on the stage. I stopped it a liitle sooner so you could actually see the plane stop. The rest of the code is basically the same. I added plane_mc.y -= 1 so the plane would not fly straight across the stage. You can play with these number as you wish to control the speed and distance the objects travel.
var yourTimer:Timer=new Timer(10,170)
yourTimer.start();
function fly (yourEvent:TimerEvent):void {
|
| |
plane_mc.x -= 5
plane_mc.y -= 1
|
};
yourTimer.addEventListener(TimerEvent.TIMER, fly) |
There are a few other event handlers that you should be aware of, but I will let you explore on your own.
updateAfterEvent - you can use this to force Flash CS3 to refress the display if you object is moving faster then the frame rate. updateAfterEvent, will result in smoother motion.

TimerEvent.TIMER_COMPLETE - you can track the end of the timer with the TIMER_COMPLETE event handler, thus being able to start another action.
Download Flash CS3 Tutorial, Source Files