AS3: Event Dispatching

As mentioned earlier, there are a number of native event classes in ActionScript 3. Each native event class has its own set of constants that define the values of specific events that can occur within the specified event class.
There is an actual native class by the name of Event, which contains basicevent types that occur in Flash, but sometimes the types offered are not enough for custom classes that need to dispatch their own events.

For this  very reason, in AS3 it is possible to create custom event objects by inheriting the Event class, just as native ActionScript event classes do.Let’s say you want to dispatch a custom event when a start button is pressed in a video player. The start button is a property in a class called VideoDisplay, and other classes can subscribe to the start button in orderto be notified when the button is pressed. Let’s start by creating a custom event class, named VideoDisplayEvent.

package
{
import flash.events.Event;
class VideoDisplayEvent extends Event
{
public static const START:String = “start”;
public function VideoDisplayEvent(type:String)
{
super(type, true);
}

public override function clone():Event
{
return new VideoDisplayEvent(type);
}
}
}

When the VideoDisplayEvent class is created, the VideoDisplay class can start dispatching events. The START event type will notify listeners when the start button is pressed in the video display. First, the VideoDisplayEvent  needs to be imported. Then a dispatchEvent method can be added to the start button’s click event in order to fire a new VideoDisplayEvent. The VideoDisplayEvent takes a property defining what type of event it is dispatching.
In this case, the event is start, represented by the START constant in the VideoDisplayEvent class.

package
{
import flash.display.*;
import flash.events.MouseEvent;
import VideoDisplayEvent;
public class VideoDisplay extends MovieClip
{
public function VideoDisplay()
{
startButton.addEventListener(MouseEvent.CLICK,
onStart);
}
public function onStart(event:MouseEvent):void
{
this.dispatchEvent(new VideoDisplayEvent
(VideoDisplayEvent.START));
}
}
}

Other classes can subscribe to the events dispatched by the VideoDisplay.
The following Display class is a document class that imports both the VideoDisplay and VideoDisplayEvent classes. The VideoDisplay class is attached to a movie clip in the Flash library so that the Display class has the ability to attach VideoDisplay instances to the stage. Since the VideoDisplay class definition contains an event dispatcher, VideoDisplay
instances can subscribe to the START event type by adding an event listener.
At this time, a custom class method can be the callback function when the START event is triggered, as in the following example with the onVideoStart method. Now that a custom event exists, you can even type the event parameter as VideoDisplayEvent in the callback function.

package
{
import flash.display.*;
import VideoDisplay;
import VideoDisplayEvent;
public class Display extends MovieClip
{
public function Display()
{
var vd:VideoDisplay = VideoDisplay
(addChild(new VideoDisplay()));
vd.addEventListener(VideoDisplayEvent.START,
onVideoStart);
}
public function onVideoStart(event:VideoDisplayEvent)
:void
{
trace(“The video display start button has been
clicked”);
}
}
}
, , , ,

No comments yet.

Leave a Reply