Activity 2: Vertical Platform Game

 

  source: http://www.kirupa.com/developer/actionscript/vertical_shooter.htm 

 Creating a Vertical Shooter
       by Ernesto Quezada aka _Bruno : January 22 2006 Vertical shooters are also known as Fixed Shooter.

A fixed shooter has players only able to control their two-dimensional position on the screen and sometimes the direction they are facing*

*Wikipedia

So, we will create a game using our super hero Pawel to kill dragons that are coming from one side of the screen. I have provided an FLA with all of the graphics you will need, but you will need to place the graphics in the right spot. This tutorial is more of an explanation of the code with some design work as opposed to a fully-featured tutorial covering every aspect of a vertical shooter.

The following animation is an example of a vertical shooter. Click inside the animation to give it focus. Use your left and right keys to move left and right, and press the up key to fire an arrow at a dragon.

Let’s Start
Because there are some sprites in the game, I have provided them for you. That way, you can focus more on the Flash as opposed to the actual design work. Download and open the following FLA for the sprites:

sprites

Now that you have your list of sprites, let’s continue. We need a sprite to make it walk, in this case I use a movieclip of Pawel (see sprite.fla) and as he is a knight, the instance name is knight:

On another layer, in the first frame of our timeline copy and paste the following ActionScript:

//—- variables —-
var steps:Number = 5;
var spriteX:Number = 265;
var spriteY:Number = 265;
//—- functions —-
function checkKeys() {
if (Key.isDown(Key.RIGHT)) {
spriteX += steps;
} else if (Key.isDown(Key.LEFT)) {
spriteX -= steps;
}
}
function updatePawel() {
knight._x = spriteX;
knight._y = spriteY;
}
this.onEnterFrame = function() {
checkKeys();
updatePawel();
};

Ok, our hero is moving from side to side on the screen but, he can even move outside of the stage! We can animate the legs and the arms so it will look better and it will add the option of throwing arrows (we don’t have arrows yet).

If you double click on your knight movie clip, you should be in the timeline for that movie clip. You will see three layers corresponding to the body, arms, and legs:

More importantly, notice that the arms and legs are movie clips with the instance names…arms and legs! If you look inside the arms and legs movie clips, you will find that they have some keyframes that provide some extra animation for movement.

Let’s modify our code to take advantage of the new movement in the legs and arms:

//—- variables —-
var steps:Number = 5;
var spriteX:Number = 265;
var spriteY:Number = 265;
//—- functions —-
function checkKeys() {
if (Key.isDown(Key.RIGHT) && spriteX<510) {
spriteX -= steps;
knight.legs.play();
} else if (Key.isDown(Key.LEFT) && spriteX>40) {
spriteX -= steps;
knight.legs.play();
}
if (Key.isDown(Key.UP)) {
knight.arms.play();
}
}
function updatePawel() {
knight._x = spriteX;
knight._y = spriteY;
}
this.onEnterFrame = function() {
checkKeys();
updatePawel();
};

In the next page, I will explain the above code and guide you through adding features such as the background and arrows.

I started explaining the code for getting our hero Pawel to move. We will keep making modifications to our code, but in this page, I will finish up my code explanation.Let’s go through the code you used in the previous page:


//—- variables —-
var steps:Number = 5;
var spriteX:Number = 265;
var spriteY:Number = 265;

First we declare the variables, steps are the number of pixels our hero will move, spriteX and spriteY will help us to give value to the X and Y position of our hero.


The first function checks if we are pressing the keys that are making our hero to do things:

if (Key.isDown(Key.RIGHT) && spriteX<510) {
spriteX += steps;
knight.legs.play();
}

The above code essentially says that only if we press the Right arrowKey and our hero is not outside of the stage, our hero will move to the right (remember, steps = 5 pixels) and play the legs animation.


else if (Key.isDown(Key.LEFT) && spriteX>40) {
spriteX -= steps;
knight.legs.play();
}

This code is the same as above, except I now check for the Left arrow key and ensure that our hero is not outside the left edge of our stage.
If we press the Up arrowKey on our keyboard, the animation in our arms movieclip will play.


function updatePawel() {
knight._x = spriteX;
knight._y = spriteY;
}

This function tells Flash that our hero will take the X and Y values of the variables spriteX and spriteY.


this.onEnterFrame = function() {
checkKeys();
updatePawel();
};

This onEnterFrame event checks to see if we are pressing keys on the keyboard or if our hero is moving.

let’s starting making our game more realistic.Adding a Background
Now we are able to move our sprite, but our sprite is “in the air.” We will fix that by adding a background.To add a background is easy as you already know. Just add another layer in the main timeline and draw your background (I drew mine in illustrator and pasted it into Flash):

If you don’t have time to create a background, a giant colored rectangle should suffice for now.

Drawing the Arrow
The arrow can be drawn in Flash as well (I used Photoshop to draw mine).  If you take a look at my FLA, you will see the arrow movie clip and image in the Library. Whether you create your own arrow, or use my arrow, just ensure that your arrow is inside a movie clip. The arrow should be a movieclip and we should leave it in the library, not on the stage.

Open the Library (Ctrl + L) and right click the arrow movieclip so you can open the Linkage Properties box. From the Linkage Properties dialog box, if you tick in linkage options Export for Actionscript by default the identifier will be arrow, so leave it like that.

Getting back to the Code
Let’s modify the code we added in the previous page. Let’s add to the script a couple of variables: one to declare the speed of our arrow and another to allow us to shoot one arrow at a time. Now, we also need a function to update our arrow.

Now the script look like:

//—- variables —-
var steps:Number = 5;
var spriteX:Number = 265;
var spriteY:Number = 265;
var speed:Number = 25;
var arrowActive:Boolean = false;
//—- functions —-
function checkKeys() {
if (Key.isDown(Key.RIGHT) && spriteX<510)>40) {
spriteX -= steps;
knight.legs.play();
}
if (Key.isDown(Key.UP) && arrowActive == false) {
knight.arms.play();
attachMovie(“arrow”, “arrows”, 8);
arrows._x = spriteX;
arrows._y = spriteY+50;
arrowActive = true
}
}
function updatePawel() {
knight._x = spriteX;
knight._y = spriteY;
}
function updateArrow() {
if (arrowActive == true) {
arrows._y -= speed;
}
if (arrows._y<-10) {
arrowActive = false;
removeMovieClip(arrows);
}
}
this.onEnterFrame = function() {
checkKeys();
updatePawel();
updateArrow();
};

Our game is progressing nicely so far. We just need to add our dragon. In this page, we will deal with the dragon and wrap up what we have done so far.

Adding the Dragon!
Pawel shoots arrows, moves and there is a beautiful background (hopefully), but Pawel is shooting into an empty sky! We need the dragons.

Again, as we did with the arrow, background, etc. we need to draw our dragons, I drew mine first using Freehand and then pasted into Flash. You can find a dragon movie clip in my sprite FLA’s library, so don’t worry if you aren’t familiar with drawing a dragon.

Here is how my dragon looks like:

[ our dragon – scary thing, ehh? ]

For the sake of this tutorial, let’s leave it as an image. You can make an animation of the wings/tail/etc. if you really really want to.

We don’t need instances of our dragon on the stage but we need it in the Library as a movieclip. Like we did with the arrow, let’s change it’s Linkage Properties.

Open the Library and right click on the dragons movieclip to open the Linkage Properties window, tick Export for Actionscript, and leave the identifier as default (dragon).

Back to the ActionScript
We now need to add some more variables:

var dragons:Number = 3;
var i:Number = 0;
var score:Number = 0;

dragons is the number of dragons we will have on the stage.

The variable i is a bit difficult to explain right now, for it does several things. Hopefully with the rest of the code, you will see what i does.

score is to store our score, we can add a dynamic text box on the stage with variable name score so we know how good we are killing dragons.


We need to add two new functions, one to initialize the dragons and another one to update them. Add the following code to the end of your existing code:

function initDragons() {
for (i; i<dragons; i++) {
attachMovie(“dragon”, “dragon”+i, i);
dragon = _root[“dragon”+i];
updateDragons(dragon);
dragon.onEnterFrame = function() {
if (this.hitTest(arrows)) {
score += 5;
trace(score);
arrowActive = false;
removeMovieClip(arrows);
updateDragons(this);
}
if (this._x>0) {
this._x -= this.velo;
} else {
updateDragons(this);
}
};
}
}
initDragons();
function updateDragons(which) {
which.gotoAndStop(random(4));
which._x = random(100)+530;
which._y = random(80)+20;
which.velo = random(10)+2;
}

In the function initDragons(), we start with the loop for (i; i<dragons; i++), usually this sort of loops look the same, with a variable to initialize the loop, the condition to loop and then increment the variable value after each loop iteration.

When the value of i is less than the number of dragons, we will attach an instance of our movieclip dragon with this script:
attachMovie(“dragon”, “dragon”+i, i)

The new name of this instance is “dragon”+i, the value of i is incrementing, so the first one will be called dragon0, the second dragon1 and because we set dragons number as 3, the last instance will be dragon2 finishing the loop.

To make our life easier, we will set a variable to store the name of each dragon, dragon = _root[“dragon”+i];

To update the dragons that have been just created, we need to call the function updateDragons(dragon).

if (this.hitTest(arrows)): this line checks if the arrows hit them, and if it happens, we increment the score by 5: score += 5;

arrowActive = false: sets the arrow’s status to be inactive

removeMovieClip(arrows): because the arrow has already hit something, we can safely remove it. This line does that.

updateDragons(this): we will update the dragons using the soon to be explained updateDragons function.


if (this._x>0) {
this._x -= this.velo;
} else {
updateDragons(this);
}

if the dragon is on the stage, then its x position is bigger than zero. We will set it to move horizontally (x axis), but if the x position is not greater then zero, then we send that dragon to our updateDragons function.


The updateDragons function is pretty straightforward. It is run whenever a dragon has been hit with an arrow or if it moves outside of the stage. We simply try to simulate a new dragon by specifying a new vertical position, horizontal position, and speed.

Your final code should look like the following:

//—- variables —-
var steps:Number = 5;
var spriteX:Number = 265;
var spriteY:Number = 265;
var speed:Number = 25;
var arrowActive:Boolean = false;
var dragons:Number = 3;
var i:Number = 0;
var score:Number = 0;
//—- properties —-
knight.swapDepths(10);
//—- functions —-
function checkKeys() {
if (Key.isDown(Key.RIGHT) && spriteX<510) {
spriteX += steps;
knight.legs.play();
} else if (Key.isDown(Key.LEFT) && spriteX>40) {
spriteX -= steps;
knight.legs.play();
}
if (Key.isDown(Key.UP) && arrowActive == false) {
knight.arms.play();
attachMovie(“arrow”, “arrows”, 8);
arrows._x = spriteX;
arrows._y = spriteY+50;
//arrowActive = true
}
}
function updatePawel() {
knight._x = spriteX;
knight._y = spriteY;
}
function updateArrow() {
if (arrowActive == true) {
arrows._y -= speed;
}
if (arrows._y<-10) {
arrowActive = false;
removeMovieClip(arrows);
}
}
function initDragons() {
for (i; i<dragons; i++) {
attachMovie(“dragon”, “dragon”+i, i);
dragon = _root[“dragon”+i];
updateDragons(dragon);
dragon.onEnterFrame = function() {
if (this.hitTest(arrows)) {
score += 5;
trace(score);
arrowActive = false;
removeMovieClip(arrows);
updateDragons(this);
}
if (this._x>0) {
this._x -= this.velo;
} else {
updateDragons(this);
}
};
}
}
initDragons();
function updateDragons(which) {
which._x = random(100)+530;
which._y = random(80)+20;
which.velo = random(10)+2;
}
this.onEnterFrame = function() {
checkKeys();
updatePawel();
updateArrow();
};

You are done!