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

Health
Now for health we need to do multiple things. We firstly need to make another dynamic text box with the instance name health. We then need to set all our variables.

So, on the frame we will need to add:

score.text=0;
health.text=100;

Then on our obstacles we need to check if the health is smaller than or equal to 0, which is italics (you only need this on one obstacle) then we need to check that they are not dead before we set health down, so insert this:

onClipEvent (enterFrame) {
if (this.hitTest(_root.char)) {
if (_root.health.text<=0) {
_root.dead = true;
}
if (!_root.dead) {
_root.health.text -= 5;
_root._x = 0;
_root.char.speed = 0;
}
}
}

And we need to make the health move with the screen, so with the character edit in:

if (dir == "right" && !_root.leftblock.hitTest(this._x+20, this._y, true)) {
_root.health._x += speed;
_root.score._x += speed;
this._x += speed;
_root._x -= speed;
}
if (dir == "left" && !_root.rightblock.hitTest(this._x-20, this._y, true)) {
_root.health._x += speed;
_root.score._x += speed;
this._x += speed;
_root._x -= speed;
}

To set the health up with a pickup you would put _root.health.text += 1 instead of the score so:

onClipEvent (enterFrame) {
if (this.hitTest(_root.char)) {
_root.health.text += 1;
unloadMovie(this);
}
}

Pick up and Shoot Gun
Now we get to the hardest part of the whole game, being able to pick up, and then shoot a gun. From the start, when I was creating my gunning abilities:
First of all, I drew my picture of a gun, made that a MC, and gave it a code a code similar to the pickup items codes but setting a variable (gotgun) true, and then this is later checked and if it is true it will fire.

Here it is:

onClipEvent (enterFrame) {
if (this.hitTest(_root.char)) {
_root.gotgun = true;
unloadMovie(this);
}
}

I then had to draw in my shooting frames into my character MC, and then gave it a frame label of shoot. After that I added in 2 points of codes to my character.

  1. onClipEvent (load) {
    jumping = true;
    speed = 0;
    maxmove = 15;
    jump = 0;
    _root.maxshoottime = 100;
    }
  2. onClipEvent (enterFrame) {
    if (!_root.ground.hitTest(this._x, this._y, true) && !jumping) {
    this._y += 6;
    jump = 0;
    jumping = true;
    }
    if (!_root.shooting) {
    _root.timer = 0;
    _root.mvsp = _xscale/20;
    }
  3. } else if (Key.isDown(Key.CONTROL)) {
    this.gotoAndStop("attack");
    attacking = true;
    speed = 0;
    } else if (Key.isDown(Key.SPACE)) {
    if (_root.gotgun == true && !_root.shooting) {
    _root.attachMovie("bullet", "bulleter", 1, {_x:_root.char._x, _y:_root.char._y-25});
    _root.shooting = true;
    with (_root.bulleter) {
    onEnterFrame = function () {
    if (_root.timer>_root.maxshoottime) {
    _root.shooting = false;
    unloadMovie(this);
    }
    _root.timer++;
    _x += _root.mvsp;
    };
    }
    speed = 0;
    this.gotoAndStop("shoot");
    }
    } else if (speed<1 && speed>-1 && !attacking) {

You now have to draw your bullet. Once you have drawn it press F8 to make it a MC. Then open your library, find your bullet, Right-click it, go to linkage and click it. Then select the checkbox next to “Export for Action Script”. Now go to the Identifier field and type bullet.

One final thing is, you may notice that after a while of playing your game all your objects go out of place. To fix that you would insert in a code to re-enforce them. To re-enforce objects when moving your _root timeline you need to set their x position to the position in which they started at minus the current position of the _root.

So for that you need to place in a code into your characters load actions to get the starting x co-ordinates of your objects. We will also get the y position of your character for when he dies.

onClipEvent (load) {
jumping = false;
speed = 0;
maxmove = 15;
healthX = _root.health._x;
scoreX = _root.score._x;
Xpos = this._x;
Ypos = this._y;
_root.maxshoottime = 100;
}

And in the enterFrame events you re-enforce their positions:

onClipEvent (enterFrame) {
_x = Xpos-_root._x;
_root.score._x = scoreX-_root._x;
_root.health._x = healthX-_root._x;
if (!_root.ground.hitTest(this._x, this._y, true) && !jumping) {
---

Test, and you should now have a nice platform game! If you have any questions, feel free to post them on the kirupaForums.

Nathan Stockton


 


page 5 of 5


 




SUPPORTERS:

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