Creating a run key with an energy bar – Part 1


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!

Advertisements
Explore posts in the same categories: ActionScript 2.0, Basic

Tags: , , , , ,

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: