从组件获取 Button 在 indexOf 数组上的点击位置

Get Button click position on indexOf array from a component

提前谢谢你, 单击按钮后,我正在尝试获取按钮在阵列上的位置。该按钮是组组件的一部分。

组件是这样的:

<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);" />

并在应用程序中尝试获取此:

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();" />

这真的可能吗?

event.currentTarget 引用被单击的 Button,但您没有将该 Button 推入数组。您将 comp1 推入数组,这实际上是该 Button 的父级。

选项 1 - 将按钮而不是父按钮推入数组:

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);
}

选项 2 - 对父项而不是子项执行索引检查:

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);
}