Ferenc Dajka
Ferenc Dajka

Reputation: 1051

Using multiple or only one eventlisteners in actionscript 3

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

Upvotes: 1

Views: 1209

Answers (2)

influjensbahr
influjensbahr

Reputation: 4078

I think you will need to add both eventListeners. See here: addEventListener(type:String, listener:Function) they are working with the string representation of those constants you are defining. I do not know what happens if those strings are the same. You can write yourself a function like this to save some code:

function addAllEvents(obj:Object, fun:Function) {
   obj.addEventListener(StylingEvent.BLUE_BG, fun);
   obj.addEventListener(StylingEvent.GREEN_BG, fun);
}

Upvotes: 0

Sunil D.
Sunil D.

Reputation: 18193

As shown above, yes, you would need to add the event listener twice.

To add only one event listener, you could modify your event class so that there is only one event name:

public static const BACKGROUND_CHANGED = "backgroundChanged";

Then add a property to your event class that stores the color:

public var backgroundColor:uint;

When it's time to dispatch the event, specify the color:

var event:StylingEvent = new StylingEvent();
event.backgroundColor = 0x0000FF;
dispatchEvent(event);

Upvotes: 5

Related Questions