Results 1 to 2 of 2

Thread: AS3 health bar

  1. #1

    AS3 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

  2. #2
    make sure healthBar_mc registration point is on the left. Also fixed the function on how it probably should be.
    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);
          }
       }
    it's not about following tutorials, they're for you to understand as3 programming so you can do it yourself.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012