Creating a Platform Game, Page 4
       by nathan stockton  |  10 September 2005

Attacking
What good are enemies without being able to kill them? Yes, they’re not too fun here’s how we will kill them. Go back into the character MC and insert another frame with the label attack. In this frame insert a picture of you enemy and a MC at the hit point where the enemy will die on. On this hit point image make the _alpha 0, and then give it the instance name attack point. From here we need to do three more things, insert 2 code fragments on to your character, and one on the enemy.

The first part we need to put on your character is to check if they press control:

} else if (Key.isDown(Key.RIGHT)) {
if (speed<maxmove) {
speed++;
}
dir = "right";
this._xscale = 100;
this.gotoAndStop("run");
} else if (Key.isDown(Key.CONTROL)) {
this.gotoAndStop("attack");
attacking = true;
speed = 0;
}

Then the next part is to go on your characters actions also, but this stops it from going to the idle frame which it will do, due to our code that sets the character idle when the speed is below 1 and greater that negative 1. So we need to add this into our code:

} else if (speed<1 && speed>-1 && !attacking) {
speed = 0;
this.gotoAndStop("idle");
}

The last code is for the enemy, it needs to check if the enemy is hitting our attack point, which we placed on, our characters attack frame. Insert this on the enemy object:

onClipEvent (enterFrame) {
if (this.hitTest(_root.char.attackpoint)) {
enemyspeed = 0;
enemystepsright = 0;
enemystepsleft = 0;
dead = true;
this.gotoAndStop("dead");
}
if (this.hitTest(_root.char) && !dead) {
_root.char.jumping = false;
_root.dead = true;
}

When doing this it is vital you put the italic part (the part that kills you character) after the bold part, as the script reads down, and it sets that you enemy is dead before it hits your character.

Now we have set attacking true, we need to set it false, when control is released. So add this to the bottom of your char.

if (_root.ground.hitTest(this._x, this._y, true) && falling) {
jump = 12;
jumping = false;
falling = false;
}
}
}
onClipEvent (keyUp) {
// on Key Up
if (Key.getCode() == Key.CONTROL) {
// if the release is control
attacking = false;
// attacking is false
}
}

More Animations
In both your character and enemy MCs you need to put it a new frame, with the label dead. This is the frame where it shows your dead characters.

Obstacles
Probably the easiest things in the game to do are the obstacles such as spikes, etc. With this code we will not kill the enemy but set the _root timeline position back to 0 and send the character to a specified position and then resets all our variables. All you need to do is insert your obstacle image, make it a MC, and give it this code:

onClipEvent (enterFrame) {
if (this.hitTest(_root.char)) {
_root._x = 0;
_root.char._x = _root.char.startX;
_root.char._y = _root.char.startY;
_root.char._y = _root.char.startY+Stage.height/2;
_root.char.speed = 0;
}
}

We also need our characters’ Y position to be collected at the start of the game, so flash knows where to moves the character to:

onClipEvent (load) {
jumping = true;
speed = 0;
maxmove = 15;
Ypos = this._y;
jump = 0;
}

Onwards to the next page!
 


page 4 of 5


 




SUPPORTERS:

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