Game Development Reference
In-Depth Information
Here, we are adding a listener to
any
instance of a
CustomEventButtonId
called
CustomEventButtonId.BUTTON_ID
. In this way, all of the screens can share the same event and
the same listener in the
Main
class. We have already taken a quick look at the
okButtonClickedListener
function. It uses the
id
value of the
Event
object passed from the
CustomEventButtonId
instance in a
switch/case
statement to change to the next state after the
button click.
The constructor must also call the constructor of its parent class,
Event
, by making the call to
super
and passing in the parameters from its own constructor:
super(type, bubbles,cancelable);
The
this.id
variable is set by taking the passed in
id
and assigning it:
this.id = id;
The clone function definition for the CustomEventButtonId class
The clone function is a part of the preexisting
Event
class. It must be declared with
override
,
because the
type
and
attributes
properties of out
CustomEvent
must be added to the clone
function.
public override function clone():Event {
return new CustomEvent(type,id, bubbles,cancelable)
}
The
clone
function is standard in the
Event
class, but we have to override it to make sure we pass
the
attributes
into the call to create a new version of the
CustomEvent
. The
attributes
object is
our own creation, so we must make sure that we change the original clone function and include it.
The CustomLevelScreenUpdate class
The
CustomLevelScreenUpdate
Event
class is very similar to to
CustomEventButtonId
class. It is
used to pass a new text value back to the listening function. Specifically, it is used by the
GameFrameWork.as
to add text to the
leveininScreen
on each new level.
Create the
CustomEventLevelScreenUpdate.as
file in the framework package structure we created
earlier:
/source/classes/com/efg/framework/CustoEventLevelScreenUpdate.as
Here is the entire code listing for this class:
package com.efg.framework
{
import flash.events.Event;
/**
* ...
* @author Jeff Fulton
*/
public class CustomEventLevelScreenUpdate extends Event {
public static const UPDATE_TEXT:String = "update level text";
public var text:String;
public function CustomEventLevelScreenUpdate(type:String,
text:String,bubbles:Boolean=false,cancelable:Boolean=false) {