# AS3 health bar

**URL:** <https://forum.kirupa.com/t/as3-health-bar/322179>\
**Category:** flash\
**Created:** [May 13, 2011, 1:59am UTC](https://forum.kirupa.com/t/as3-health-bar/322179 "2011-05-13T01:59:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![robotronic](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@robotronic](https://forum.kirupa.com/u/robotronic)\
**Post date:** [May 13, 2011, 1:59am UTC](https://forum.kirupa.com/t/as3-health-bar/322179/1 "2011-05-13T01:59:54Z")

</div>

Ive seen a lot of tutorials but none are really what im looking for. I have a square right now which is saved as troop1\_mc. Inside that MC i have a green bar called healthBar\_mc.

Pretty much how can I get healthBar\_mc to decrease health whether its scaleX or alpha.x or whatever. Ill Attach/copy the applicable code in. It would really help. Im still new to AS3.

THANKS!!!

play\_btn.addEventListener(MouseEvent.CLICK, begin);  
var infantry:Number = 2; // the speed at which object moves  
var infantryHealth:Number = 100;  
var healthBar = troop1\_mc.healthBar\_mc;

function begin (yourEvent:Event):void {  
addChild(troop1\_mc);  
var castleDamage:Number = Math.random() \*10;  
var castleDamageRounded= Math.round(castleDamage);

```
troop1_mc.x += infantry; 
troop1_mc.addEventListener(Event.ENTER_FRAME, begin)

if (troop1_mc.x &gt;= 300){
troop1_mc.removeEventListener(Event.ENTER_FRAME, begin)
} 

//Start Damage 
setTimeout(causeDamage,5000);
function causeDamage() {
infantryHealth -= castleDamageRounded;

    if (infantryHealth &gt;= -10){
    
    trace(infantryHealth);
    troop1_mc.removeEventListener(Event.ENTER_FRAME, begin)
    
    } 
    removeChild(troop1_mc);

}

```

play\_btn.removeEventListener(MouseEvent.CLICK, begin)  
} //Closes function begin
