5 Replies Latest reply on Aug 26, 2009 10:31 AM by Flex harUI

    Specify a hitArea for a button

    watern

      Whenever I try to specify the hitArea for a button, it doesn't work. Here is my code:

       

      <s:Button hitArea="{mySprite}" skinClass="components.MyButton" id="myButton" click="buttonClicked(event);"/>

       

      I have mySprite defined in my as code outside any function, then in an init() function that gets called after creationComplete(), I set the size of mySprite.

       

      It prevents the button from using it's default hitArea, but it doesn't use mySprite.  Is there something I am missing in my process?

       

      Thanks for any help.

        • 1. Re: Specify a hitArea for a button
          Flex harUI Adobe Employee

          How did you create and size mySprite?

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Specify a hitArea for a button
            watern Level 1

            Hi Flex harUI,

             

            To create mySprite, in an external .as file linked to the .mxml file through a script tag, I used:

             

            private var mySprite:Sprite = new Sprite();

             

            I had a creationComplete event for the mxml file to call the function init():

             

            public function init():void

            {

                 mySprite.width = 200;

                 mySprite.height = 50;

            }

             

            Do I need to do anything else with mySprite to get it to work as the hitArea?

             

            Thanks for your time.

            • 3. Re: Specify a hitArea for a button
              Flex harUI Adobe Employee

              New Sprites are balloons.  If you stretch them by assigning them a width/height, they snap back right away.  You have to fill them with graphics first, then they'll hold a size.  And once filled with graphics, setting width/height scales them.

               

              I rarely set width/height on Sprites unless I don't care about the scale.  I often just fill them with a rectangle.

               

              beginFill()

              drawRect(0, 0, 200, 200);

              endFill()

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 4. Re: Specify a hitArea for a button
                watern Level 1

                Thank you Flex harUI, that was an excellent explanation of a Sprite, I could really visualize how they behave with the balloon analogy.

                 

                But unfortunately I still can't get my button to use the sprite as the hitArea.  I now have in my .as file:

                 

                mySprite.graphics.beginFill(0x00FF00);

                mySprite.graphics.drawRect(0, 0, 200, 50);

                mySprite.graphics.endFill();

                 

                but myButton acts as though it has no hitArea.

                 

                I also tried adding mySprite to a UIComponent and adding it to the stage with addElement(), but that didn't work either.

                • 5. Re: Specify a hitArea for a button
                  Flex harUI Adobe Employee

                  We normally do not use hitArea so I don't know all of its idiosyncracies.  The examples say you have to set mouseEnabled=false and add it to the displaylist, and that usually means its position is important as well.

                   

                  We recommend shaping the buttonskin to match your hit area.

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui