Saturday, June 25, 2011

Creating custom events using Actionscript 3: Part 2

Last time we've learned the basics of creating custom events in AS3 - we have the idea on how the process goes. Today we'll learn how to make more complex custom event classes.

In the previous tutorial we have created a custom event class which only supports one type of events. We have an imaginary NPC that can say things, and we can listen to the SAY_MESSAGE event of the NPCEvent class to react to it and display the message. Let's add another event type to this class - SHOUT_MESSAGE.

First of all, we'll need to go to our NPCEvent class and add a new public static constant SHOUT_MESSAGE.

public static const SHOUT_MESSAGE:String = "shoutMessage";

We'll also change the Message string variable to Parameters object variable. This way we can pass as many values as we want. We'll be able to pass the NPC's message using this, as well as the NPC's name and perhaps some other information as well.

public var Parameters:Object;

Now, the super() method in this case requires 3 parameters - event type, bubbles and cancelable. In the last tutorial we've only specified one - the type. We'll modify the NPCEvent function so that it now has 4 parameters - 2 required and 2 optional. The required ones are type:String and param:Object. We'll use them to pass the event type, for example, NPCEvent.SAY_MESSAGE, and a param object containing the message, for example, {Message: "Hello"}. The 2 optional parameters will be bubbles and cancelable, which are both set to false by default, but we will be able to change them if we want.

public function NPCEvent(type:String, param:Object, bubbles:Boolean = false, cancelable:Boolean = false)

So we'll have to super the type, bubbles and cancelable parameters, and then set the Parameters variable to the param value.

public function NPCEvent(type:String, param:Object, bubbles:Boolean = false, cancelable:Boolean = false) {
super(type, bubbles, cancelable);
this.Parameters = param;
}

So the full class code will be:

package  {
import flash.events.Event;

public class NPCEvent extends flash.events.Event{
public static const SAY_MESSAGE:String = "sayMessage";
public static const SHOUT_MESSAGE:String = "shoutMessage";
 public var Parameters:Object;

public function NPCEvent(type:String, param:Object, bubbles:Boolean = false, cancelable:Boolean = false) {
super(type, bubbles, cancelable);
this.Parameters = param;
}

}

}


Now, back to our main project file, we will need to add 2 separate listeners for 2 types of events - SAY_MESSAGE and SHOUT_MESSAGE.

addEventListener(NPCEvent.SAY_MESSAGE, sayHandler);

function sayHandler(evt:NPCEvent):void
{
trace("NPC said: " + evt.Parameters.Message);
}

addEventListener(NPCEvent.SHOUT_MESSAGE, shoutHandler);

function shoutHandler(evt:NPCEvent):void
{
trace("NPC shouted: " + evt.Parameters.Message);
}

We can dispatch the events using the dispatchEvent method and passing a new NPCEvent to it, which can have 4 parameters - type, param, bubles and cancelable. In my example, I'm only using the first two.

dispatchEvent(new NPCEvent(NPCEvent.SAY_MESSAGE,{Message: "I'm saying this!"}));
dispatchEvent(new NPCEvent(NPCEvent.SHOUT_MESSAGE,{Message: "I'm yelling this!"}));

And that's how you create custom events in AS3.

Thanks for reading!

Related:

Creating custom events using Actionscript 3: Part 1

1 comment:

Saket said...

like it... cool stuff

Post a Comment