Posted tagged ‘health’

Picking up health, making enemies and moving a character.

August 3, 2008

Welcome to my first tutorial. This tutorial is a basic tutorial which shows you how to make a health pack, create some enemies and create a simple walking script for a simple flash game.

STEP 1:

Make two different MC’s. Give one of them the instance name hero. The hero MC will be your character that you control in your game. The other one (the health pack) doesn’t need any instance name.

STEP 2:

These MC’s do need some graphical work inside them, so draw a health pack inside the health pack MC and draw a player inside the hero MC.

STEP 3:

Now, let’s begin with making the player walking with the arrow keys. This is very simple and I’m now gonna explain for you how to do it.

Please, take some time to actually understand the code and don’t just copy/paste. If you do that, you will never learn!

onClipEvent (load) { //When the MC have loaded
power = 3; //The speed of the player
}
onClipEvent (enterFrame) { //When you enter the frame, this code will run itself on and on again
if (Key.isDown(Key.LEFT)) { //When you hold down the left key
_x -= power; //x position will go lower according to the "power" variable
this._rotation = 270; //This will rotate the MC 270 degrees.
play(); //Play the walking animation, if you have any
}
if (Key.isDown(Key.RIGHT)) { //When you hold down the right key
_x += power; //x position will go higher
according to the "power" variable
this._rotation = 90; //This will rotate the MC 90 degrees.
play(); //Play walking animation
}
if (Key.isDown(Key.UP)) { //When you hold down the up key
_y -= power;
//y position will go lower according to the "power" variable
this._rotation = 0; //No rotation needed, will reset rotation if the MC was already rotated
play(); //Play the walking animation
}
if (Key.isDown(Key.DOWN)) { //If you hold down the down key
_y += power;
//y position will go higher according to the "power" variable
this._rotation = 180; //Rotate the MC 180 degrees.
play(); //Play the walking animation
}
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP)) { //If you hold down both the right and up key
_rotation = 45; //Rotate the MC 45 degrees.
}
if (Key.isDown(Key.LEFT) && Key.isDown(Key.UP)) { //If you hold down both the left and up key
_rotation = 315; //Rotate the MC 315 degrees.
}
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN)) { //If you hold down both the right and down key
_rotation = 135; //Rotate the MC 135 degrees.
}
if (Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN)) { //If you hold down both the left and the down key
_rotation = 225; //Rotate the MC 225 degrees
} //END!

STEP 4:

Now, let’s click on the first frame on the main timeline. We will now add a health variable. It’s only one line code.

health=100; //This means you have 100 HP's

STEP 5:

Now, let’s add a dynamic text box. Insert a text field, and change it to dynamic in the drop down menu. Then after that, you will see a text field called “Var:” under the properties. Change var to health. If you now test your movie (CTRL+ENTER), you can see that there is the number 100 in the dynamic text field.

STEP 6:

We was talking about making an enemy. This is easy, as long as we don’t want any AI for it. We mustn’t use anything more complicated than hitTest for this. So, create a third MC. This will be the enemy. You must also have some code on it, of course.

The code isn’t many lines, and it’s easy to learn and understand, study, and don’t just copy/paste!

onClipEvent(enterFrame){ //When you enter the frame, code will run over and over again
if(_root.hero.hitTest(_x, _y, true)){ //Checks if the hero MC hits this.
_root.health-=10; //You'll lose 10 health in case you touch this enemy.
}
}

Now, that wasn’t hard, right?

STEP 7:

We’re almost finished, but we must add the health pack first. To begin, double click the health pack MC. Then press the first frame and open the actions panel. Then just write stop(); which will stop the MC from playing.

Now, add a blank keyframe inside the health pack. Then, just return to the main timeline and add the following code (but no copy/paste, I think you’re starting to get it!) to the health pack MC.

onClipEvent (enterFrame) { //When you enter the frame, code will run itself over and over again.
if (_root.hero.hitTest(_x, _y, true)) { //Checks if the hero MC hits the healthpack.
this.gotoAndStop(2);//If it's true, then this healthpack will go to the empty frame, so it can't be picked up again.
if (_root.health<70) { //If your health is less than 70...
_root.health += 30; //Then, you'll get 30 HP's
} else { //But. what if the health is over 70/=70?
_root.health = 100; //Then it will be 100. Full health.

}
}
}

Great, now everything should work! Look out for more tutorials by me!

Advertisements