The whole deal with event dispatching is that you listen for certain event to 
take place from the outside, not from the inside.

The way you have it now, you're not really making use of event dispatching 
(well only in an weird way), but it's more like adding 
(overriding) a method of the class.

instance.onEvent=function(){}

You should do something like this:

import mx.utils.Delegate;
function eventHandler(o:Object) {
    trace("EVENT ::: eventHandler");
}
var etry:EventTry=new EventTry();
etry.addEventListener("eventName", Delegate.create(this, this.eventHandler));

The only problem you might then run into is that if you try to dispatch an 
event in the constructor of the class (or the init method 
called from the constructor) is that the EventDispacher mixin has not taken 
place yet. There's a delay (1 frame?) you have to take 
into account. You can get around that by using a setInterval or if the class 
extends the MovieClip class, by defining an onLoad 
method in the class and dispatch the event there.

regards,
Muzak

----- Original Message ----- 
From: "Javier Tello" <[EMAIL PROTECTED]>
To: "Flashcoders Mailing List" <[email protected]>
Sent: Sunday, November 13, 2005 2:33 PM
Subject: [Flashcoders] dispatching events from a class


> Hello, I'm having troubles implementing the EventDispatcher class with flash8.
>
> I do the same I use to do before, but now the class doesn't fired the event. 
> Here is a sample class:
> ___
>
> import mx.events.EventDispatcher;
> //
> class  EventTry {
>
> //event functions;
> private var dispatchEvent:Function;
> private var addEventListener:Function;
> private var removeEventListener:Function;
> //
> //fired events;
> public var onEvent:Function;
>
> public function EventTry(){
>
> EventDispatcher.initialize(this);
> this.addEventListener("onEvent",this);
> init();
> }
> //
> private function init(){
> dispatchEvent({type:"onEvent", target:this});
> die();
> }
> private function die(){
> removeEventListener("onEvent",this);
> this=null;
> delete this;
> trace("EventTry die");
> }
> }
> ___
>
> And here is the fla code:
>
> import com.mg.temp.EventTry;
> //
> var etry:EventTry=new EventTry();
> etry.onEvent=function(){
> trace("onEvent fired");
> }
> //end
>
> ___
> Here I doesn't get the "etry. onEvent" function fired. Also no error, it just 
> ignore the statement.
> Curiously my older classes run well with such a code.
> Any help would be appreciate,
> thanks,
> Javier.


_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to