所以我在舞台上有很多按钮,并且id喜欢为所有按钮分配相同的功能。
我希望函数本身评估收到的事件是否是MOUSE_CLICK或MOUSE_HOVER,并根据事件做某事......
我拥有的是这样的......
btn1.addEventListener(MouseEvent.CLICK, makeListener('option1'));
btn2.addEventListener(MouseEvent.CLICK, makeListener('option2'));
btn3.addEventListener(MouseEvent.CLICK, makeListener('option3'));
btn4.addEventListener(MouseEvent.CLICK, makeListener('option4'));
btn5.addEventListener(MouseEvent.CLICK, makeListener('option5')); // and so on for about 100 buttons
btn1.addEventListener(MouseEvent.MOUSE_HOVER, makeListener('option1'));
btn2.addEventListener(MouseEvent.MOUSE_HOVER, makeListener('option2'));
btn3.addEventListener(MouseEvent.MOUSE_HOVER, makeListener('option3'));
btn4.addEventListener(MouseEvent.MOUSE_HOVER, makeListener('option4'));
btn5.addEventListener(MouseEvent.MOUSE_HOVER, makeListener('option5')); // and so on for about the same 100 buttons
function makeListener(option: String): Function {
return function (event: Event): void {
// DO something
}
}
我想要的是这样的东西......(显然它不起作用)
btn1.addEventListener(MouseEvent.CLICK || MouseEvent.CLICK, makeListener('option1'));
btn2.addEventListener(MouseEvent.CLICK || MouseEvent.CLICK, makeListener('option2'));
btn3.addEventListener(MouseEvent.CLICK || MouseEvent.CLICK, makeListener('option3'));
btn4.addEventListener(MouseEvent.CLICK || MouseEvent.CLICK, makeListener('option4'));
btn5.addEventListener(MouseEvent.CLICK || MouseEvent.CLICK, makeListener('option5')); // and so on for about 100 buttons
function makeListener(option: String): Function {
return function (event: Event): void {
if (event = MOUSE_CLICK){
// DO something
} else if(event = MOUSE_HOVER){
// Do Something else
}
}
}
有什么想法吗?
答案 0 :(得分:0)
我会在按钮类和事件处理程序中添加CLICK
和MOUSE_HOVER
个侦听器,以便调度CustomEvent
。
接下来,在您正在倾听所有CLICK
和HOVER
个事件的舞台上,我将addEventListener
为此自定义Event
并在其中一个参数中传递您想要的操作要做。
所以,你需要ButtonCustomEvent
类使用const和extra param:
public class ButtonCustomEvent extends Event
{
public static const CUSTOM_EVENT:String = "ButtonCustomEvent::CUSTOM_EVENT";
private var _customParam:String;
public function ButtonCustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
public function get customParam():String
{
return _customParam;
}
public function set customParam(value:String):void
{
_customParam = value;
}
}
按钮类中的下一步:
public class MyButtonClass extends Sprite
{
public function MyButtonClass ()
{
super();
addEventListener(MouseEvent.CLICK, handleEvent);
addEventListener(MouseEvent.MOUSE_OVER, handleEvent);
}
public function handleEvent(event:MouseEvent):void
{
var myEvent:ButtonCustomEvent = new ButtonCustomEvent(ButtonCustomEvent.CUSTOM_EVENT, true);
myEvent.customParam = event.type;
dispatchEvent(myEvent);
}
}
然后,如果您要为~100个按钮添加侦听器的类,您现在可以执行:
addEventListener(ButtonCustomEvent.CUSTOM_EVENT, handleEvent);
public function handleEvent(event:ButtonCustomEvent):void
{
if(event.customParam == MouseEvent.CLICK)
{
//DO something
}
else if(event.customParam = MouseEvent.MOUSE_OVER)
{
//Do Something else
}
}