Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

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