In Actionscript 2, one of the best ways to communicate between classes is with EventDispatcher. On this page, I'll show an example of a very simple application that uses two classes, Sender and Receiver, which communicate via EventDispatcher (which is built into Flash 7 and 8, but requires an import statement to use). These two classes can be used as a starting point for any application that requires communication via EventDispatcher.
Any class which must dispatch (broadcast) events should be given a structure like this:
/**
* @class Sender
* @author Helen Triolo
* @description Example of code needed to enable a class to broadcast events using
* EventDispatcher. All the code in this class needs to be included
* in a dispatching class, except the setInterval line (which is just
* included here to force an actual event broadcast). The name of the
* event ("eventFired") should be changed to something appropriate
* (eg, shooterFired, or handleDragged), and any number of variables
* may be passed (not just the one, elapsedTime, shown here), of any
* legitimate dataType.
* @language Actionscript 2.0
*
*/
import mx.events.EventDispatcher;
class Sender {
// these three lines are needed to use EventDispatcher
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
public function Sender() {
// this line must be in the constructor of the class
EventDispatcher.initialize(this);
// dispatch an event once per second
setInterval(this, "sendEvent", 1000);
}
private function sendEvent():Void {
dispatchEvent({target:this, type:"eventFired", elapsedTime:getTimer()});
}
}
in which The properties in the object sent by dispatchEvent include:
The only line that is specific to this example is the setInterval line, which is included to actually cause an event to be dispatched -- in this case, once every second.
The Receiver class looks like this:
/**
* @class Receiver
* @author Helen Triolo
* @description Example of code needed to enable a class to listen for
* events broadcast by another class and respond to them
* @language Actionscript 2.0
*
*/
import mx.utils.Delegate;
class Receiver {
private var timeline:MovieClip;
public function Receiver($timeline:MovieClip) {
timeline = $timeline;
var sender:Sender = new Sender();
sender.addEventListener("eventFired", Delegate.create(this, onEventFired));
}
public function onEventFired(o:Object):Void {
timeline.msg.text = "You've been here for " + Math.round(o.elapsedTime/1000) + " seconds";
}
}
The only lines that need to be included in a receiving class areIn this case, the responding function simply collects the one variable passed back, elapsedTime, and displays it with a message in the msg textfield. Notice that variables passed via dispatchEvent are available as properties of the one parameter, a variable of type Object, of the responding function. This variable is always available and can be named with any name you like -- I used "o" for simplicity. So o contains all of the information passed back by the dispatching class, and I can pull whatever variables were named in the dispatchEvent call as properties, eg, o.elapsedTime
Once the Receiver.as file and Sender.as file have been created, the only thing the fla needs is a call to instantiate the Receiver (which creates its own Sender instance), and whatever graphic content is required to support the classes -- this case, a dynamic textfield named msg. An instance of Receiver is created, and a reference to the current timeline passed to it, with this line in frame 1 of the fla:
var rec:Receiver = new Receiver(this);
You can download the files used in this example at the right with the "free download" link. To see an example of EventDispatcher used in a simple game application, see the class-based version of the Dart Shooter Game.
Discussed on this page:
using built-in eventDispatcher to communicate between classes, dispatchEvent, broadcast, listener, sender, receiver
Files:
testEventDispatcher.fla
Receiver.as
Sender.as
(free download)
A list of all files currently available at the site may be viewed here.