News - Happenings at iGeeks

September 25th, 2004

EventDispatcher

Kenneth J Toley, III of Macromedia have a great article on Creating Events Using the EventDispatcher Class.

The article discusses the basics of using the EventDispatcher class and provides the steps for building custom classes that broadcast custom events. “While most components and objects in Macromedia Flash MX 2004 have built-in support for UI events (mouse, keyboard, etc.) and Load or Data events (load, complete, progress, etc.), these events alone may not always meet developers’ needs. There are times when you may want to detect a complex change in application state, build custom components, or just force methods to wait for the completion of a lengthy process. Often Flash developers resort to the use of global variables or convoluted loops to repeatedly check for state change with limited success.”

Brajeshwar

August 24th, 2004

Eventdispatcher

Let me share my knowledge in ‘Eventdispatcher’ in flash, as I found very
useful in creating my own components.
Let me just refresh in actionscript 1.0 ”ASBroadcaster

Have u ever happened to use ASBroadcaster?

The ASBroadcaster object itself, despite its presumed intimidation, is
simply a generic object with one method, initialize. This is used on other
objects to transform those objects into broadcasting capable objects. In
doing so, it adds three new methods and a new property to the transformed
object. These methods are addListener, removeListener, broadcastMessage and
the property, _listeners. addListener lets you add a listener to your new
broadcasting object. removeListener lets you remove one. broadcastMessage is
the command that the initialized object runs to generate an event to be sent
out to the listeners (like onMouseDown) while _listeners is a list of all
the listeners associated with the broadcast object. So, for the usage of
ASBroadcaster, we get:

ASBroadcaster.initialize(objToBroadcast);
objToBroadcast.addListener(listenObj);
objToBroadcast.removeListener(listenObj);
objToBroadcast.broadcastMessage(”event”);
objToBroadcast._listeners;

ASBroadcaster, used internally by FlashMX (not in FLASHMX2004 or
professional) for broadcasting Messages to handle built in events such as(onMouseDown,onKeyUp…)

Major architecture change in ASbroadCaster,one level up called “EventDispatcher” in flashMX2004.

The methods of the EventDispatcher class let you add and remove event
listeners so that your code can react to events appropriately
.”
So let us stop theory,theory of no use unless we implement it practically.

Creating your own events:
———————————-

Open a text editor (if u are using flashMX2004) or select Actionscript File
from FlashMx Professional 2004.

i) save MyButton.as and copy paste the below code:

/* let us extend our class MyButton to Movieclip,because Movieclip(top level) is mother of all UIOBJECTS AND UICOMPONENTS (i.e UIOBJECTS AND UICOMPONENTS inherits from Movieclip)*/
class MyButton extends MovieClip {
// declare dispatchEvent it as function for a COMPILER to know they exist.
private var dispatchEvent:Function;
// constructor function
public function MyButton(Void) {
// The simplest way to enable your components to dispatch events to listeners is to use the same mechanism as the v2 components use.
mx.events.EventDispatcher.initialize(this);
}
// onRelease dispatchEvent specify event and target
function onRelease(Void):Void {
dispatchEvent({type:”click”, target:this});
}
}
// end of MyButton class.

ii)Now open new FLA and save it in a relative path reference to MyButton.as

create any Movieclip symbol and type the class name”MyButton” in fielder AS2.0 class(just to extend our movieclip to our class)

drag the instance and name it as “but”

select first frame in our timeline and press F9 to open actionscript panel
and paste the below code.

// import is optional if path is relative,better to specify fully qualified
path to avoid confusion if u wish to use packages later.

import MyButton;
var oListener:Object = new Object();

oListener.click = function(oEvent:Object) {

  trace(oEvent.target._name);

};

but.addEventListener(”click”, oListener);

Just Press Ctrl-Enter,u will see u have successfully added your own event
to your movieclip.

Advantage:

1)  u can easily add and remove events by using V2 architecture.

2)  Take advantage of macromedia - engineers effort.

NOTE:

The above illustration is not specific to any application,it is just to
demonstrate the simple usage of EVENTDISPATCHER.

Santosh

Comments

There are no comments for this post so far.

Post a comment

Policy on Comments : First-time comments are automatically put under moderation (and will not be displayed) until they are approved. However, your comments in future will show up immediately.

Join iGeeks

Sign up with your email to be an iGeek

Managed by Yahoo Groups

If you wish to advertize on iGeeks, please contact the manager for the same.

Contribute

iGeeks would love to publish your technical articles; contact the manager if you wish to write for iGeeks.

Contact

brajeshwar [at] brajeshwar.com