In this tutorial, you will learn how to extend the native Event class to create your own custom events. Though there are many styles and forms this class can achieve, this tutorial will give you the basic way of creating one.


The very basic look of a custom event class is like this.

package Prezire.Mobiles.Games.HiddenObjectGames.Events{
	import flash.events.Event;
	public class ItemEvent extends Event{
		public function ItemEvent(type:String, 
						bubbles:Boolean, 
						cancelable:Boolean = false){
			super(type, bubbles, cancelable);
		}
	}
}

And to use it would be like:

this.dispatchEvent(new ItemEvent("onFound"));

One that’s vaguely used is the concept of creating a literal value in the first parameter. This of course is acceptable however, is considered unreliable for the reason that it could be misspelled. To solve this, you can create a static variable inside the class.

public static FOUND:String = "onFound";

This makes the whole class look like this.

package Prezire.Mobiles.Games.HiddenObjectGames.Events{
	import flash.events.Event;
	public class ItemEvent extends Event{
		public static FOUND:String = "onFound";
		public function ItemEvent(type:String, 
						bubbles:Boolean, 
						cancelable:Boolean = false){
			super(type, bubbles, cancelable);
		}
	}
}

Once the event is dispatched, the listener of that event can then correctly identify, which type of event is firing. In this case “onFound”.

private final function itemEventListener(e:ItemEvent):void{
	switch(e.type){
		case ItemEvent.FOUND:
			//Animate the target to fade out.
		break;
	}
}

In this example, the letter “n” of the word onFound is capitalized. This doesn’t produce an error however, rest assured that your switch-case’s condition won’t be met.

private final function itemEventListener(e:ItemEvent):void{
	switch(e.type){
		case "oNFound":
			//Animate the target to fade out.
		break;
	}
}

Avoid creating another property inside a custom event class unless it is absolutely necessary. In this example, a bonus method is added on any random occassion once the event is dispatched.

package Prezire.Mobiles.Games.HiddenObjectGames.Events{
	import flash.events.Event;
	public class ItemEvent extends Event{
		public static FOUND:String = "onFound";
		private var iBonus:int;
		public function ItemEvent(type:String, 
						bubbles:Boolean, 
						cancelable:Boolean = false){
			super(type, bubbles, cancelable);
		}
		public final function set bonus(value:int):void{
			this.iBonus = value;
		}
		public final function get bonus():int{
			return this.iBonus;
		}
	}
}

Refer to ie.bonus when adding a bonus value during dispatching.

private final function itemClickListener(e:MouseEvent):void{
	var i:Item = Item(e.target);
	switch(e.type){
		case MouseEvent.CLICK:
			var ie:ItemEvent = new ItemEvent(ItemEvent.FOUND);
			if(Math.random() * 3 < 1){
				ie.bonus = 300;
			}
			i.dispatchEvent(ie);
		break;
	}
}

The example above is acceptable however, why not just place the “bonus” method inside the Item class itself like so?

package Prezire.Mobiles.Games.HiddenObjectGames{
	import flash.displays.MovieClip;
	public class Item extends MovieClip{
		private var iBonus:int;
		public function Item(){
			//
		}
		public final function set bonus(value:int):void{
			this.iBonus = value;
		}
		public final function get bonus():int{
			return this.iBonus;
		}
	}
}

And set the bonus value during dispatch like so:

private final function itemClickListener(e:MouseEvent):void{
	var i:Item = Item(e.target);
	switch(e.type){
		case MouseEvent.CLICK:
			if(Math.random() * 3 < 1){
				i.bonus = 300;
			}
			i.dispatchEvent(new ItemEvent(ItemEvent.FOUND));
		}
	}
}

Congratulations! You have successfully created your own custom event class that is considered reliable, efficient and suitable for the class that needs it.

Advertisements