Creating a Vertical Shooter - Page 4
       by Ernesto Quezada aka _Bruno : January 22 2006

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! If you have any questions, please visit the forums.

Salut!

_Bruno

 


page 4 of 4


 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.