1

Sorry for the lame question but I don't know how to search for it. So if I have two events from the same class like:

package  {
import flash.events.Event;

public class StylingEvent extends Event{
    public static const BLUE_BG:String = "blue_bg";
    public static const GREEN_BG:String = "green_bg";

    public function StylingEvent(type:String) {
        super(type);
    }
}}

Do i need to add two eventlisteners like:

gameData.addEventListener(StylingEvent.BLUE_BG, onChangeBg);
gameData.addEventListener(StylingEvent.GREEN_BG, onChangeBg);

Or is it possible like:

gameData.addEventListener( [any type of stylingEvent] , [some method]);

Thanks

4

2 回答 2

5

如上所示,是的,您需要添加两次事件侦听器。

要仅添加一个事件侦听器,您可以修改您的事件类,以便只有一个事件名称:

public static const BACKGROUND_CHANGED = "backgroundChanged";

然后向存储颜色的事件类添加一个属性:

public var backgroundColor:uint;

当需要调度事件时,指定颜色:

var event:StylingEvent = new StylingEvent();
event.backgroundColor = 0x0000FF;
dispatchEvent(event);
于 2012-08-27T17:41:38.207 回答
0

我认为您将需要添加两个事件侦听器。请参见此处:addEventListener(type:String, listener:Function)他们正在使用您正在定义的那些常量的字符串表示形式。我不知道如果这些字符串相同会发生什么。你可以自己写一个这样的函数来保存一些代码:

function addAllEvents(obj:Object, fun:Function) {
   obj.addEventListener(StylingEvent.BLUE_BG, fun);
   obj.addEventListener(StylingEvent.GREEN_BG, fun);
}
于 2012-08-27T17:41:49.950 回答