8 Replies Latest reply on Nov 2, 2011 2:28 PM by MatteoLegittimo

    getChildIndex Error

    MatteoLegittimo Level 1

      What's wrang with my code in  my flex mobile project?

       

       

      <?xml version="1.0" encoding="utf-8"?>

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

              xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView" xmlns:local="*">

         

          <fx:Script>

              <![CDATA[

                  protected function mouseDownListener(event:MouseEvent):void {

                      trace(brickContainer.getChildIndex(MovieClip(event.target)))

                  }

              ]]>

          </fx:Script>

         

          <fx:Declarations>

              <!-- Place non-visual elements (e.g., services, value objects) here -->

          </fx:Declarations>   

          <s:Group id="brickContainer" mouseDown="mouseDownListener(event)">

              <s:layout>

                  <s:HorizontalLayout/>

              </s:layout>

              <local:BrickAsset/>

              <local:BrickAsset/>

              <local:BrickAsset/>

          </s:Group>

      </s:View>

       

       

       

       

      I get this error in mouseDownListener:

       

      Main Thread (Suspended: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.)

       

      Thanks a lot!

        • 1. Re: getChildIndex Error
          Flex harUI Adobe Employee

          Mouse targets in a container are not guaranteed to be parented by the container.  They can be buried deeper because of skins or because of hierarchy in the target component itself.

          • 2. Re: getChildIndex Error
            MatteoLegittimo Level 1

            thanks for your replay!

            How can i do?

             

            thanks again!

            • 3. Re: getChildIndex Error
              Shongrunden Adobe Employee

              Does getElementIndex() work?

              • 4. Re: getChildIndex Error
                saisri2k2 Level 4

                Probably add mouseDown on all the children and remove the mouseDown on the parent, the same handler should be good.

                 

                    <fx:Script>

                        <![CDATA[

                            protected function mouseDownListener(event:MouseEvent):void {

                                trace(brickContainer.getChildIndex(event.target as BrickAsset))

                            }

                        ]]>

                    </fx:Script>

                  

                  

                    <s:Group id="brickContainer" >

                        <s:layout>

                            <s:HorizontalLayout/>

                        </s:layout>

                        <local:BrickAsset mouseDown="mouseDownListener(event)"/>

                        <local:BrickAsset mouseDown="mouseDownListener(event)"/>

                        <local:BrickAsset mouseDown="mouseDownListener(event)"/>

                1 person found this helpful
                • 5. Re: getChildIndex Error
                  MatteoLegittimo Level 1

                  no!

                  not working

                  • 6. Re: getChildIndex Error
                    MatteoLegittimo Level 1

                    Like this it's working:

                     

                    <?xml version="1.0" encoding="utf-8"?>

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

                            xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView" xmlns:local="*">

                        <fx:Script>

                            <![CDATA[

                                protected function mouseDownListener(event:MouseEvent):void {

                                    trace(brickContainer.getElementIndex(BrickAsset(event.currentTarget)))

                                }

                            ]]>

                        </fx:Script>

                       

                        <fx:Declarations>

                            <!-- Place non-visual elements (e.g., services, value objects) here -->

                        </fx:Declarations>   

                        <s:Group id="brickContainer">

                            <s:layout>

                                <s:HorizontalLayout/>

                            </s:layout>

                            <local:BrickAsset mouseDown="mouseDownListener(event)"/>

                            <local:BrickAsset mouseDown="mouseDownListener(event)"/>

                            <local:BrickAsset mouseDown="mouseDownListener(event)"/>

                        </s:Group>

                    </s:View>

                     

                    I did mouseDownListener to the group because i would like to assign just one event handler, because the BrickAsset are added at runtime!

                    Any solution instead ?

                    Thanks!

                    • 7. Re: getChildIndex Error
                      saisri2k2 Level 4

                      You can add the listeners at run time, I see no issue with it.

                      • 8. Re: getChildIndex Error
                        MatteoLegittimo Level 1

                        I did like this in creationCompleteEvent:

                         

                        protected function view1_creationCompleteHandler(event:FlexEvent):void {

                                        var currentAsset:BrickAsset

                                        for (var i:int = 0; i < brickContainer.numChildren; i++)  {

                                            currentAsset = BrickAsset(brickContainer.getChildAt(i))

                                            currentAsset.addEventListener(MouseEvent.MOUSE_DOWN ,mouseDownListener)

                                            currentAsset.buttonMode = true

                                        }

                                    }