Continuous Game Action
Peter Hughes, Arete Designs
Several Flash Samples, including Fighter and Pan Zoom,
give hints on moving objects around on the Stage. But objects in these samples come to
a dead stop when you quit pressing the buttons. Sometimes you want your spacecraft to keep
right on going!
In this tutorial, you will make a simple 'game' with
a circle that you move back and forth with buttons. Each button press will nudge
the circle, to give it a little more speed in the direction you choose. When you
quit nudging, the circle will continue to move at a constant speed.
The circle is actually a Movie Clip with an Instance Name, that allows you to tell it what to do. This circle has a 'speed' variable you will create, and the buttons add to or subtract from this speed. The frame containing the circle plays again and again. On each play, the circle moves to a new position. The new position is merely its old position plus its current speed.
TUTORIAL |
Part 1 - Symbols
In Frame One of your timeline, place a circle on the Stage. Make it a Movie Clip symbol. In its Instance Properties > Definition tab, give it an instance name of Circle. This will allow you to tell the "/Circle" target to move around.
Make a left button and a right button.
Part 2 - Button Actions
The circle starts out with no speed. Each press of a
button should increase its speed to the left or to the right.
Add an On Mouse Event action to the left button:
On (Press)
Set Variable: "speed" = speed - 0.1
End On
Subtracting from the speed will move the circle faster to the left, or at least slow down motion to the right.
Add an On Mouse Event action to the right button:
On (Press)
Set Variable: "speed" = speed + 0.1
End On
Part 3 - Frame Actions
Add a keyframe at Frame 2 of your timeline. Give the frame an action: Go to and Play(1). This will make your movie constantly cycle back to Frame 1 and perform your circle-moving commands.
Now, add the actions to Frame 1 that will make the circle respond to its speed. You will add this speed to the old position of the circle, and make this its new position. Since in this example the circle moves only to the left or to the right, position on the X axis is all we care about.
Add an action to Frame 1:
Set Variable: "circle_x" = GetProperty("/Circle",_x)
This gets the current position of the circle. Store this position
in circle_x so you can play with it. The tricky part in all of these steps is
to make sure quote marks are correct. Everything on the right hand side of the
equal sign is set to an Expression using the radio buttons in the Action Script editor.
Add a second action to Frame 1:
Set Property ("/Circle", X Position) = circle_x + speed
This adds the circle's speed to its current position, and will make the ball move a little on this play of the frame.
Now you are ready to give it a try!
Troubleshooting
Chances are, you start the movie and click the buttons, and nothing happens. Here are some points to check.
A - Most likely one of your actions is still a String Literal on the right hand side of the equal sign. Make sure these are all set to Expression.
B - Perhaps button actions were put in the button symbol itself with the up, over, down, hit frames. Edit only the Actions tab in the Instance Properties window.
Notes and Other Tweaking
Continuous Action Buttons - In this example, one button click gives you one nudge. The Continuous Action Button tutorial elsewhere on this site will show you how to keep the rockets burning hard for as long you hold the buttons down.
Variable watch - The .fla shows how you can look at your variables as the movie plays - this will be important as you add to your game and it starts to not work!
Initializing variables - You will also want to add another frame in front of the game, to set variables to their starting values. Good luck and enjoy!
© 2000 Peter Hughes
www.arete-designs.com
Last Updated 01/23/00