简体   繁体   English

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

[英]Flash: Dynamic button click handler

I need a few pointers on how to create multiple buttons programmatically with different event handlers, or rather: differently parametrized event handlers. 我需要一些关于如何以编程方式使用不同的事件处理程序来创建多个按钮的指针,或者更确切地说是:参数化的事件处理程序。 My real use is a bit more complex, but it comes down to this: I need a button that can delete itself upon getting clicked. 我的实际用途要复杂一些,但要归结为:我需要一个按钮,可以在单击后将其删除。

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

I can't figure out the way to do this in Flash, except doing something silly like checking mouse position during the click event against all buttons, and then figuring out which one was clicked. 我无法弄清楚在Flash中执行此操作的方法,只能做一些愚蠢的事情,例如在click事件期间针对所有按钮检查鼠标位置,然后找出被点击的按钮。

You could do something else, but similar: 您可以做其他事情,但是类似:

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

You probably should remove it from the stage too though. 您可能也应该从舞台上将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM