(This is part 4 of a series of posts on ways to pass arguments to a function through event listeners. For part one, see here, for part two, see here, and for part three, see here.)
Today sees the last chapter of our quest to pass information to a function through a listener. For this final method we’re going to be looking at creating our own custom events, and how they can be used to store and retrieve information.
Method 4 – Custom Events
What we’re going to be doing is taking the standard flash Event class, and extending it to fit our needs. This concept should be very familiar to you if you are aware of Object Oriented Programming. The first thing we need to do if we’re going to extend Event is import it to our own custom class, and let flash know that we want to take it and build upon it:
package { import flash.events.Event; public class MyEvent extends Event { } }
Now there are a couple of things we need to put inside of our class before we get down to the functions. The first is a static constant String which will refer to the type of event we will be triggering. This needs to be static so that it can be accessed without having to instantiate the class, and constant as it will not be changing. The second thing we need to put before our constructor is any variables we want to use to pass information – in our case, another string.
package { import flash.events.Event; public class MyEvent extends Event { public static const CUSTOM:String = "custom"; public var info:String; } }
Next up, we’re going to make our constructor function. This will accept all of the same parameters as a standard Event, as well as one extra one for our string:
public function myEvent(type:String, customInfo:String = null, bubbles:Boolean = false, cancelable:Boolean = false){ }
By placing customInfo second in the list, the third and fourth parameters remain optional, as they are in the standard listener. Also, by giving it a default value of null we ensure that if no extra information is passed, the value null will be used.
We need to do two things in the constructor function – first we need to call the standard Event so that our custom event will perform as expected, and second we need to store the extra information in myEvent:
public function myEvent(type:String, customInfo:String = null, bubbles:Boolean = false, cancelable:Boolean = false){ super(type, bubbles, cancelable); info = customInfo; }
This is the main part of the custom event finished, but there are still a couple of things to be done. We need a clone method to make sure everything still works when re-dispatching an event. This is an important part of writing a custom event, so I’m going to include the code here – however it’s not particularly relevant to our end goal, so if you want to understand more about what it does and why you need it, I suggest you start here:
public override function clone():Event { return New MyEvent(type, info, bubbles, cancelable); }
The final thing out class is missing is a ‘toString’ function. This will return a string containing all of the information on a listener. In order for it to function correctly, we want to make sure it includes all of the extra information from our custom class:
public override function toString ():String{ return formatToString("MyEvent", "type", "info", "bubbles", "cancelable", "eventPhase"); }
So now our custom event is finshed, and looks like this:
package { import flash.events.Event; public class MyEvent extends Event { public static const CUSTOM:String = "custom"; public var info:String; public function myEvent(type:String, customInfo:String = null, bubbles:Boolean = false, cancelable:Boolean = false){ super(type, bubbles, cancelable); info = customInfo; } public override function clone():Event { return New MyEvent(type, info, bubbles, cancelable); } public override function toString ():String{ return formatToString("MyEvent", "type", "info", "bubbles", "cancelable", "eventPhase"); } } }
Now, in order to reap the benefits of this class, we need to make sure we’re listening out for it:
this.addEventListener(MyEvent.CUSTOM, onCustomEvent);
Then we set up the onCustomEvent function to retrieve information from the event:
public function onCustomEvent(e:MyEvent):void { trace(e.info); }
Now we can dispatch our event, and pass in a string:
dispatchEvent(new MyEvent(MyEvent.CUSTOM, "Button 1 was pressed!"));
In doing this, you can see how this technique can be used to pass information using an event. This is the final method, and certainly the most flexible, robust, and maintainable one out there.
I hope you’ve enjoyed this series of posts on events and listeners, and if you have any questions, please don’t hesitate to leave a comment.
