Game Development Reference
In-Depth Information
public function CustomEventButtonId(type:String,
id:int, bubbles:Boolean=false, cancelable:Boolean=false){
super(type, bubbles,cancelable);
this.id = id;
}
public override function clone():Event {
return new CustomEventButtonId(type,id, bubbles,cancelable)
}
}
}
Class import and variable definition for the CustomEventButtonId
class
In this section, we set up all of the variables necessary for the class. We will create a single
constant that will be used for the button ID event.
BUTTON_ID:String = "button id"
The id String variable is used to pass data between the event and the listener.
We pass the id value of the instance of the BasicScreen class where the OK button was clicked. If
the button was on an instance of the BasicScreen class, the id (set in Main when we instantiate
the BasicScreen instance) is passed to all objects listening to the event. In the framework, Main
listens for this event and changes state based on the screen where the OK button was clicked.
The constructor definition for the CustomEventButtonId class
The first String in the constructor should be the BUTTON_ID constant defined previously. You can
also create CustomEventButtonId instances on the fly by passing a String value for the type ,
rather than one of the preset string constants, into the constructor. It is better organizationally to
use the preset constants (you can always add more) than to pass in an arbitrary String value. It
also helps eliminate runtime errors associated with an unknown or unhandled type value being
passed in. If your listener function is set to listen for an event called GAME_OVER but you mistype
the type as GAMR_OVER , you might have a difficult time debugging the fact that your game never
handles that event properly.
Let's look at the BasicScreen 's use of the CustomEvent class again:
dispatchEvent(new CustomEvent(CustomEventButtonId.BUTTON_ID,id));
When the Event is created, we pass in the CustomEventButtonId.BUTTON_ID constant to define
what event we are going to fire off, and we pass a newly created int representing the id of the
BasicScreen instance.
How does GameFrameWork.as listen for a custom Event ? Main needs to add a listener for each of
the three screens that have OK buttons. These are added when needed and deleted when not
needed. You have already seen the full text of the functions for each screen state in
GameFrameWork here is a reminder example from the title screen:
titleScreen.addEventListener(CustomEventButtonId.BUTTON_ID,
okButtonClickListener, false, 0, true);
Search WWH ::




Custom Search