2 Replies Latest reply: Oct 6, 2009 5:34 AM by Me2LoveIt2 RSS

    Drawing with ActionScript3.0(Sprite) in Flash CS4

    Me2LoveIt2 Community Member

      Hi,

      I wanted to make a scene, similar to a little game.

      I included:

      A character with an Instance Name "character_mc"

      A box1 with an Instance Name "hp_mc" (hp for health points)

      And a second box2 with an Instance Name "dmg_mc" (dmg for damage)

       

      Now I used this code to make it draw a health bar for me:

       

           var hp:Number = 100 //note this is not the complete code just the piece I want to focus on
      this.addEventListener(Event.ENTER_FRAME, hpBar);
      function hpBar(event:Event):void{
           if(character_mc.hitTestObject(hp_mc)){
                hp+=1
           }else if(character_mc.hitTestObject(dmg_mc)){
                hp-=1
           }
           var bar:Sprite = new Sprite(); 
           bar.graphics.beginFill(0x33ff00); 
           bar.graphics.drawRect(50, 50, hp, 15)
           bar.graphics.endFill(); 
           addChild(bar);
      };
      
      

      It draws me 100 pixel health, and when the character moves on the box1(hp_mc)

      I can see the health increasing (more and more pixels are drawn)

       

      The problem starts here.

       

      When I move my character onto the box2(dmg_mc). It does subtract and eventually draw in the negative, but it does not erase what was drawn before.

       

      How can I code this to have the same function but erases the previous drawing?

       

      Any seggestions are wellcome.

       

      ~Thanks for help and advice ~

        • 1. Re: Drawing with ActionScript3.0(Sprite) in Flash CS4
          Ross Ritchey Community Member

          I believe you are looking for the clear() function.  Note, I also moved the bar variable declaration out of the function so that you don't reinitialize it unnecessarily, as well as the addChild so you only add the bar to the stage once.

           

          var hp:Number = 100 //note this is not the complete code just the piece I want to focus on


          var bar:Sprite = new Sprite();
          addChild(bar);

          this.addEventListener(Event.ENTER_FRAME, hpBar); function hpBar(event:Event):void{      if(character_mc.hitTestObject(hp_mc)){           hp+=1      }else if(character_mc.hitTestObject(dmg_mc)){           hp-=1      }     

               bar.graphics.clear();//     

               bar.graphics.beginFill(0x33ff00);      bar.graphics.drawRect(50, 50, hp, 15)      bar.graphics.endFill(); };
          • 2. Re: Drawing with ActionScript3.0(Sprite) in Flash CS4
            Me2LoveIt2 Community Member

            Thank you so much this is exactly what I needed.