IE179 Project 2
Flash Game Tutorial 1
In this tutorial you'll start building a simple side scrolling games.
-
Open Flash and start a new Flash movie.
-
First we'll create a simple character for the player to control.
Use Insert->New Symbol then select Movie Clip and call it Circle (instead
of Symbol 1). In the stage create a filled circle.
-
Go back to the main stage by selecting Scene 1 (above the timeline).
-
Click on the Library pane on the right. (If it is not there, select Window->Library
and it should appear.) The Circle movie clip should be available.
-
Drag the icon for the Circle onto the stage. In the properties section
name the instance circle.
-
At this point you can test your "game" by selecting Control->Test Movie.
You should see a flash movie of your circle!!!
-
OK, so it's not that exciting. Next we'll make the circle move. First,
rename the layer containing the circle as circle. Now add a another layer
and rename it scripts. Select the scripts layer and then select the
Actions pane on the right. It should open a window that you can type in.
(If not, use the pulldown menu on the top right of the panel to change
to expert mode.)
Copy the following code to the window.
onEnterFrame=function () {
if(Key.isDown(Key.RIGHT)) {
circle._x -=3;
trace(circle._x);
}
if (Key.isDown(Key.LEFT)) {
circle._x +=3;
trace(circle_x);
}
}
-
Test your game now using the left and right arrow keys to move the circle.
The "x-position" of the circle will also print out whenever the circle moves.
-
You may want to eliminate the "trace" commands, which cause the messages to
print. Or you can put a double slash in front:
// trace(bg.x);
which comments out the line.
-
Next we'll create the background for the game.
Download the file background.JPG.
In Flash use Insert->New Symbol then select Movie Clip and name it BG.
You'll get a new stage to create the background. Use File->Import to Stage
to import the background image. Position the bit map so that the upper left
corner of the image is at the center of the stage.
-
Select Scene 1 to return to the main stage. Make a new layer called
background. Select the layer then drag the icon for the BG movie clip
from the library onto the stage. Call this instance bg.
Position the circle so that it
is sitting near the left of the background on the horizon.
If the background obscures the ball, move
the background layer below the circle layer. (In other words, grab
the panel where you named the layer and move it down.)
-
Test your game again. The ball should move across the horizon.
-
Now... what you really want to do is keep the ball ball in the center of
the screen and make the background move. See if you can figure out how
to do that!! (HINT: bg._x is the x-position of the background bit map.)
-
Now let's make the ball jump. First add code so that the ball moves
up 5 pixels when the player hits the up arrow. Hint: (Key.UP will be
useful. Also, (0,0) is at the top left corner and y increases as you
go down!)
-
Test your game. Does the ball rise when you hit the up arrow?
-
Eventually we'll add gravity and collision detection so the ball falls
until it hits the horizon. For now we can simulate the effect
as follows. Add
var height = circle._y;
to the top of the actionscript file and the following
at the beginning of the OnEnterFrame function:
if (circle._y < height) {
circle._y +=1;
}
-
Test out your game.