|
    |
|
|
Flash CS3 Tutorial on Repeat Actions
At times in Flash 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 point a to b. The Event.Enter_Frame is the line you will need. Event.ENTER_FRAME tells flash to do this or that for every frame in the movie. This continues action will happen even if the time line is stopped. It will happen as often as you 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 some of the work is done for you. I have created an image of a car and the background of the street. I 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) In the example I added an if statement that checks to see if the car has moved 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 ENTER_FRAME is not the only way to continually control an object. You can also create a timer. So lets move the Plane with a timer. This code is a little different in that you will need to create a variable. You create a variable 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 milliseconds, 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 little 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 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 trake the end of the timer with the TIMER_COMPLETE event handler, thus being able to start another action. |
Flash Tutorial
PLEASE VISIT THE CONTRIBUTOR'S WEBSITE
FrenchSquared
Website dedicated to Flash Tutorials
FrenchSquared.com
|
|
No reactions yet.
Please login or sign up to rate this intel.
Please login or sign up to add a comment.
The copyright for this content entitled "Flash CS3 Tutorial on Repeat Actions" has been specified by the contributor as:
All Rights Reserved
This content may not be copied, distributed or adapted by anyone under any circumstances.
|
 |
May, 2012
2008
January, February, March, April, May, June, July, August, September, October, November, December
2009
January, February, March, April, May, June, July, August, September, October, November, December
2010
January, February, March, April, May, June, July, August, September, October, November, December
2011
January, February, March, April, May, June, July, August, September, October, November, December
2012
January, February, March, April, May
|
|
Not a member yet?
Qondio is a powerful network for making it online. If you have a website to
promote, we can help.
Sign up and get in on the action.
|
|
Welcome to Qondio! Discover the awesome power this network can deliver by going to our About page. Or you could skip straight to the Sign Up form.
|
|