2 Replies Latest reply on Mar 25, 2011 1:54 PM by boulder_lingie

    Using hitTestObject with Buttons Added in AS

    boulder_lingie Level 1

      I would like to use hitTestObject with Spark Buttons that I create dynamically in Actionscript. I'm confused about this, though. I was testing this code:


      <s:Group id="myGroup">
      <local:RectButton id="button1" x="100" y="20" label="Button1" fill="0xff0000"/>
      <local:RectButton id="button2" x="105" y="20" label="Button2" fill="0x00ffff"/>
      </s:Group>

       

      And I had this in the creation complete function for my test application:
      trace( "button 1 hit test object button 2: " + button1.hitTestObject( button2));

       

      It returns true.

       

      When I take out the MXML for the two buttons, and add them to my creation complete function like this:
      var button1 : RectButton = new RectButton();
      button1.x = 100;
      button1.y = 20;
      button1.label = "Button1";
      button1.fill = 0xff0000;
      myGroup.addElement( button1 );

       

      var button2: RectButton = new RectButton();
      button2.x = 105;
      button2.y = 20;
      button2.label = "Button2";
      button2.fill = 0x00ffff;
      myGroup.addElement( button2 );

       

      Then this line:
      trace( "button 1 hit test object button 2: " + button1.hitTestObject( button2));
      returns FALSE!!!

       

      The RectButtonSkin is really simple. It is a SparkSkin with this in it:
      <s:Rect left="0" right="0" top="0" bottom="0" height="1" width="1" >
      <s:fill>
      <s:SolidColor id="rectFill" color="0xffffff" alpha="0.3" />
      </s:fill>
      </s:Rect>

       

      And the RectButton is a spark Button with a fill attribute.

       

      Anyone have any ideas about this?

       

      Thanks!
      Anna

        • 1. Re: Using hitTestObject with Buttons Added in AS
          Shongrunden Adobe Employee

          You might be calling hitTestObject() too early.  Try waiting for the updateComplete event on the Group, for example:

           

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                         xmlns:s="library://ns.adobe.com/flex/spark"

                         creationComplete="createElements()">

           

              <fx:Script>

                  <![CDATA[

                      import spark.components.Button;

                      import mx.events.FlexEvent;

           

                      private var asButton1:Button;

                      private var asButton2:Button;

           

                      protected function createElements():void

                      {

                          asButton1 = new Button();

                          asButton1.label="Button1";

                          asButton1.x = 100;

                          asButton1.y = 20;

                          myGroup.addElement(asButton1);

           

                          asButton2 = new Button();

                          asButton2.label="Button2";

                          asButton2.x = 105;

                          asButton2.y = 20;

                          myGroup.addElement(asButton2);

           

                          // too early

                          trace(asButton1.hitTestObject(asButton2));

           

                          // wait for updateComplete before tracing the test

                          myGroup.addEventListener(FlexEvent.UPDATE_COMPLETE, traceTest);

                      }

           

                      private function traceTest(e:Event):void {

                          trace(asButton1.hitTestObject(asButton2));

                      }

                  ]]>

              </fx:Script>

           

              <s:Group id="myGroup" />

           

          </s:Application>

          • 2. Re: Using hitTestObject with Buttons Added in AS
            boulder_lingie Level 1

            Yes - that did it! I added the code you suggested, and when I run my app, the hit test is true. Very cool - thank you!!