Posted tagged ‘walk’

Creating a run key with an energy bar – Part 1

August 17, 2008

So, in some games, you would like to be able to run, not only walk. Many games allows you to do this. Some games have got an energy bar, some haven’t. I’ll teach you how to make a walking system, with a run key and an energy bar. It might sound kinda complicated, but it isn’t. In fact, it’s very easy.

STEP 1:

Let’s start with the most important part. The walking code, which here also implements the running. It’s not much to add, to add the running feature.

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.SHIFT)){//When shift is down
power=8; //Set what the speed should be while running.
}else{//If it isn't down
power=3; //Set power to it's original value. In this case 3.
}
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
}
}

This did implement the running. Too bad, we haven’t got an energy bar yet, or any energy value in that case. The code above haven’t even gotten support for it yet, but that’s only the reason why you should change:

if(Key.isDown(Key.SHIFT)){

to

if(Key.isDown(Key.SHIFT) && _root.energy>0){

This will check if you actually have enough of energy. But wait, there’s another change to do in the code, add the following:

_root.energy-=0.5//Energy will do down. Change the variable to how fast you wish it to go down.

under the line you just changed (the one with the if Shift down bla bla….)

And yeah buddy, one last change, under the }else{, add the following code:

_root.energy+=0.3; //Energy have to go up too!

In case your lazy/didn’t understand, here’s the full code after modifications:

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.SHIFT) && _root.energy>0){//When shift is down and you have enough of energy
power=8; //Set what the speed should be while running.
_root.energy-=0.5//Energy will do down. Change the variable to how fast you wish it to go down.
}else{//If it isn't down
power=3; //Set power to it's original value. In this case 3.
_root.energy+=0.3; //Energy have to go up too!
}
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
}
}

Okey, now you got the biggest part done, go ahead to the next step!

STEP 2:

You’ve probably detected that we have NO energy variable. Right? Well, we need one, so let’s add one! Add this code to the frame! You must also prevent the variable from going higher than wanted, here’s a code for that too!

energy=100;//Now, the energy is one hundred
if(energy>100){//If energy is more than default, in this case 100
energy=100;//The application wont accept it and set it to 100
}

Now, in the next part we’ll create the energy bar, stay tuned, it’s probably coming already tomorrow!

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!