FreshNova - Home

Wednesday 10th of March 2010

Home Flash Tutorials Simple Energy/Health Bar In A Flash Game
Simple Energy/Health Bar In A Flash Game PDF Print E-mail
Written by Waleed Al-Bahrawy   
Saturday, 14 February 2009 00:15

 

This is a simple bar like the ones we see in shooting games that shows how much life,health or energy left for you, I just made a very simple example to show how it is done.

As you can see I made two buttons Shoot Me and Help!!.  when you click on Shoot Me your energy will be decreased,

if you click on Help!! it will increase.

 

What I did to make this bar is that I created a movieclip ,inside it I drew a rectangle and expanded it to frame number 20,

then created a shape tween on it. So every time Shoot Me is clicked the bar will be decreased by one frame,

and when help is clicked the bar will be increased by one frame.

 

Lets take a look at the code and see how it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//At the begining the life is full, 
//the frame number of the bar is set to the last frame,
//which is frame number 20
life.gotoAndStop(life.totalFrames); 
shootMe.addEventListener(MouseEvent.CLICK,shootMeFun);
help.addEventListener(MouseEvent.CLICK,helpFun);
 
function shootMeFun(e:MouseEvent){
	//checking if the frame number is greater than 1 
        //to decrease the frame number
	if(life.currentFrame>1){
	  life.gotoAndStop((life.currentFrame-1));
	}
	//if the frame number is 1 and got shot then you must DIE
	else{
           //this is a text on the stage that 
           //tells you the status either alive or dead
           lifeTxt.text="Dead"; 
           //disable the shooting and help buttons, 
           //YOU ARE DEAD NOW
	  shootMe.removeEventListener(MouseEvent.CLICK,shootMeFun);
	  help.removeEventListener(MouseEvent.CLICK,helpFun);
	}
 
}
 
function helpFun(e:MouseEvent){
	//if you still alive and your energy is not complete
	if(life.currentFrame<life.totalFrames){
	  life.gotoAndStop((life.currentFrame+1));
	}
}

 

The code is documneted and I will be happy to answer any question..

 

The source file is in the attachments below.

 

Attachments:
FileFile sizeLast Modified
Download this file (LifeBar.fla)LifeBar64 Kb14/02/09 13:39

Comments

avatar Ben
0
 
 
Hi there,
I am making a little game and this code is perfect for what I need. However, instead of making the health increase or decrease upon the click of a button I would like it to go up and down when triggered by different content embedded in each frame. So the content of one frame could add 5 health, and the content of another could decrease it by 1, is this possible?

All the best,
Ben
Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Phoebe
-1
 
 
Good morning. Paradise is exactly like where you are right now... only much, much better.
I am from Afghanistan and learning to read in English, please tell me right I wrote the following sentence: "This academia has reported canada to some decision to available sound elements.All conditions sold from momentum computations will be designated into a role bank depreciation until they can be found eventually into the financial fund.Also the hands-on excess time has been accumulated closely and with them the legal style practice.Regarding to the finance, if all balances include on company, the second will edit about the new escrow of this office."

Thanks :(. Phoebe.
Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Waleed Al-Bahrawy
0
 
 
Hey Ben,
Do you want to change the amount the life lose or gain?
Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Greg
0
 
 
Creating a simple shooting game where a player can shoot bottles going by on a conveyor belt. I have an energy/ammo bar that should go down as the player clicks the mouse . When I shoot the 1st bottle in my game, the ammo bar will decrease, but only after I have shot that 1st bottle. When I click "Play Again", my ammo bar never returns to a full level. Here is the code:

stage.addEventListene r(MouseEvent.CLICK, updateAmmo);

function updateAmmo(event:MouseEvent){

// we need to check to see if the player has run out of bullets
if (ammo_mc.bar_mc.scaleX

// reset our game variables
BottlesCreated = 0;
BottlesDestroye d = 0;
ammo_mc.bar_mc.scaleX = 1; // reset the players energy level
score = 0;
bottlesToWin = 10;


// and lastly, start making enemies again!
BottleMaker = setInterval(createBottle, 750);
}
// ---------START GAME----------- --------------- --------------- --
initializeGame();
Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Waleed Al-Bahrawy
0
 
 
Hi Greg,

The bar is not reset to its full length because of this event " stage.addEventListene r(MouseEvent.CLICK, updateAmmo); "

What is happening is when you click on "Play Again", " updateAmmo " will be called because "Play Again" is part of the stage.

The solution to that is to stop " updateAmmo " from being called when the reply button is clicked by removing the event listener > stage.addEventListene r(MouseEvent.CLICK, updateAmmo);

in your reset section to the following:

1- replace "ammo_mc.bar_mc.scaleX = 1; // reset the players energy level" by "resetBar();"

// reset our game variables
BottlesCreated = 0;
BottlesDestroye d = 0;

resetBar();

score = 0;
bottlesToWin = 10;

2- add this function:

function resetBar(){
//remove the stage event to stop the bar from decreasing
stage.removeEventList ener(MouseEvent.CLICK, updateAmmo);

//I'm using a timer to call "wait", which will reset the scaleX to 1 and add back the stage event
//the timer will give the game some time to apply removing the stage event
var t:Timer=new Timer(1,1);
t.addEventListene r(TimerEvent.TIMER_COMPLETE,wait);
t.start();
}

3- add this function:

function wait(t:TimerEvent){
ammo_mc.bar_mc.scaleX=1;
stage.addEventListene r(MouseEvent.CLICK, updateAmmo);
}
Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Sam
0
 
 
Nice idea. After the movie Matrix it is populat to give a choice to people in such a form. But of course I maybe mistaken. Well, what I really see, is that this idea is very nice but it needs to be developed. You know I read a lot about peoples habit. There are very nice book by Allan and Barbara Pease, I found some at the pdf search engine http://pdf.rapid4me.com . In their books I find explanation to our deeds.
Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Last Updated on Saturday, 14 February 2009 14:28
 

Archive

 

Categories


Copyright © 2009 FreshNova, by Talha Kalim and Waleed Al-Bahrawy