3 Replies Latest reply on Aug 18, 2010 3:53 PM by kglad

    Making the health bar go down (AS2)

    Spaghetee

      Alright, so after some work and a lot of mistakes, I made a health bar for my game. (This is in Actionscript 2)

      Basically, the code is on my main character's .as file, there is
      var maxHealth : Number = 100;
      var health : Number = 100;

      and further down the line...
      _root.healthBar._width = (health/maxHealth) * 200;

      ~~~~~~~~

      As a result, when I change the var health number lower, the health bar goes lower. Sweet! It works!

      Now I was wondering if any of you guys could help me code it so an enemy can inflict damage upon the main character.
      I have tried many codes but they have all failed.

      if(this.hitTest(_root.Monster))
      {

      }

      What would I put in the brackets?

      Thanks

        • 1. Re: Making the health bar go down (AS2)
          kglad Adobe Community Professional & MVP

          :

           



          if(this.hitTest(_root.Monster))
          {
          this.health -= 10;  // for 10 damage

          // and always check that health doesn't go below 0 and when it reaches 0, you take some action
          }

          • 2. Re: Making the health bar go down (AS2)
            Spaghetee Level 1

            EDIT: Never mind. I had a stupid little "health = 100;" I put there a long time ago.

             

            Now the only problem is that the health -=10 just doesn't work

            (Health bar simply does not move when colliding with monster... o.o)

             

            EDIT2: Agh! I think I got it! It doesn't detect the collision with the monster because the monster does not have the instance name of "Monster".

             

            How do you give something an instance name if it doesn't appear until a while? (There are waves of these monsters so they do not appear for a while, and they continously come in waves)

             

            EDIT3: YES! I solved it! Basically, I went to the monster's .as file and included the code there

             

            Same thing with the hittest, but for the effect I did _root.Mainchar.health -=10 instead of this.health -=10

             

            Sweet! Thanks

            • 3. Re: Making the health bar go down (AS2)
              kglad Adobe Community Professional & MVP

              you're welcome.