Game Development Reference
In-Depth Information
Part 2: Events
A core component of ActionScript 3 is the use of events. Event
objects can be believed as messages that are sent between objects
to notify each other of, well, events. When an object is set up to
receive events, it is known as listening . When an object sends an
event, it is known as dispatching . In their most basic form, events
object that dispatched the event), and a currentTarget (the object
currently processing the event after receiving it). A basic event is
merely a notification that something happened, and you
ll need to
access the object that sent the event in order to get any more infor-
mation. However, events can be customized to send any amount of
data along with the message, but we
ll get to that shortly. First, let
look at how objects can send events.
Many of the core classes of AS3 dispatch events. In order to
dispatch an event, an object must either extend the Event-
Dispatcher class in some way or implement the IEventDispatcher
interface (see the interface example in the previous section on
classes). If it meets one of these two criteria, you simply call the
dispatchEvent method and pass it an event object. Event objects
are created from the Event class or a sub class of it. The basic
Event class has a number of predefined names, or enumerations ,of
event types, but you can use any name you want.
var event:Event = new Event(Event.COMPLETE);
var event:Event = new Event( " myCustomEventName " );
However, it is a good idea to define your event names as con-
stants somewhere so that you can avoid misspellings. For instance,
if my game class needed to tell another object when the game had
started and ended, it would be wise for me to define these event
names, so they can be referenced later. The typical event naming
scheme is to use all capital letters, with underscores between
words, for the property name and CamelCase for the actual value,
as in the next example.
package {
import flash.display.Sprite;
public class Game extends Sprite {
static public const GAME_START:String = " gameStart " ;
static public const GAME_OVER:String = " gameOver " ;
Search Nedrilad ::

Custom Search