Results 1 to 2 of 2
Thread: AS3 health bar
-
May 12th, 2011, 09:59 PM #12Registered User
postsAS3 health bar
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 >= 300){
troop1_mc.removeEventListener(Event.ENTER_FRAME, begin)
}
//Start Damage
setTimeout(causeDamage,5000);
function causeDamage() {
infantryHealth -= castleDamageRounded;
if (infantryHealth >= -10){
trace(infantryHealth);
troop1_mc.removeEventListener(Event.ENTER_FRAME, begin)
}
removeChild(troop1_mc);
}
play_btn.removeEventListener(MouseEvent.CLICK, begin)
} //Closes function begin
-
May 13th, 2011, 01:38 AM #2
make sure healthBar_mc registration point is on the left. Also fixed the function on how it probably should be.
it's not about following tutorials, they're for you to understand as3 programming so you can do it yourself.Code:function causeDamage() { infantryHealth -= castleDamageRounded; troop1_mc.healthBar_mc.scaleX=infantryHealth/100; if (infantryHealth <= 0){ trace(infantryHealth); troop1_mc.removeEventListener(Event.ENTER_FRAME, begin) removeChild(troop1_mc); } }

Reply With Quote


Bookmarks