Flash:动态按钮单击处理程序

时间:2011-06-18 10:46:36

标签: flash actionscript-3 user-interface button

我需要一些关于如何使用不同的事件处理程序以编程方式创建多个按钮的指针,或者更确切地说:不同的参数化事件处理程序。我真正的用途有点复杂,但它归结为:我需要一个可以在点击时删除自己的按钮。

var Buttons:Vector.<Button> = new Vector.<Button>;      
var newButton = new Button;
var int = Buttons.push(newButton);              
newButton.addEventListener(MouseEvent.CLICK, button_clickHandler);


// pseudocode
button_clickHandler(event:MouseEvent):void {
    if (event.button = i) { delete button i}
}

我无法弄清楚在Flash中执行此操作的方法,除了做一些愚蠢的事情,比如在针对所有按钮的点击事件期间检查鼠标位置,然后确定点击了哪一个。

1 个答案:

答案 0 :(得分:4)

你可以做别的事情,但类似的事情:

private function buttonClickHandler(event:MouseEvent):void
{
      var button:Button = Button(event.target);
      var i:int = buttons.indexOf(button);
      // now you know the button instance and the index of the button so do whatever you need

      // delete it from the vector:
      buttons.splice(i, 1);
}

你可能也应该从舞台上删除它。

相关问题