1

Thank you in advance, I'm trying get the position of a button on the array after the button was click. The button is part of a group component.

the component is this:

<fx:Metadata>
    [Event(name="eventoConcluido", type="flash.events.Event")]
</fx:Metadata>
<fx:Script>
    <![CDATA[
        protected function endEventBtn(event:MouseEvent):void
        {
            var eventTerminado:Event = new Event("eventoConcluido");
            dispatchEvent(eventTerminado);

            //this.removeAllElements();
        }
    ]]>
</fx:Script>

<s:Button id="endEvent" label="Test" color="black" width="100" click="endEventBtn(event);" />

And in application try to get this:

private var myArray:Array = [];
private var myButton:Button;
protected var comp1:test;

public function addButton():void {  

    comp1= new test();
    myGroup.addElement(comp1);
    myArray.push(myGroup.addElement(comp1));
    comp1.endEvent.addEventListener(MouseEvent.CLICK, getButton);
}

public function getButton(event:MouseEvent):void {
    var ij:int= myArray.indexOf(event.currentTarget);
    trace(ij)// always get -1;
}



<s:Button label="Add Button" click="addButton();" />

Is this actually possible?

1 Answer 1

1

event.currentTarget references the Button being clicked on, but you didn't push that Button into the Array. You pushed comp1 into the Array, which is actually that Button's parent.

Option 1 - Push the Button into the array instead of the parent:

public function addButton():void {
    comp1 = new test();
    myGroup.addElement(comp1);
    myArray.push(comp1.endEvent);
    comp1.endEvent.addEventListener(MouseEvent.CLICK, getButton);
}

public function getButton(event:MouseEvent):void {
    var ij:int= myArray.indexOf(event.currentTarget);
}

Option 2 - Do the index check on the parent instead of the child:

public function addButton():void {
    comp1 = new test();
    myGroup.addElement(comp1);
    myArray.push(comp1);
    comp1.endEvent.addEventListener(MouseEvent.CLICK, getButton);
}

public function getButton(event:MouseEvent):void {
    var ij:int= myArray.indexOf(event.currentTarget.owner);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you Karma, work perfect the Option2. I´m very grateful for your help.
You're welcome. Please mark the question as answered if you are satisfied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.