Hi, guys.
I´m with serious problem here. I´m studying AS3 for a few days and still cant do an Event system work properly.
After googling for a while I stopped here:
// Listener Class:
package{
import flash.events.IEventDispatcher;
import flash.events.Event;
public class ListenerObject{
private var label:String;
public function ListenerObject(label:String){
this.label = label;
}
public function addDispatcher(dispatcher:IEventDispatcher):void{
dispatcher.addEventListener("testEvent", testEventHandler);
}
public function toString():String{
return "[ Listener "+label+" ]";
}
private function testEventHandler(evt:Event):void{
trace(evt.type+" dispatched from "+evt.target.toString()+" to "+this.toString());
}
}
}
// Dispatcher code:
package{
import flash.events.EventDispatcher;
import flash.events.Event;
public class DispatcherObject extends EventDispatcher{
private var label:String;
public function DispatcherObject(label:String){
this.label = label;
}
public override function toString():String{
return "[ Dispatcher "+label+" ]";
}
public function dispatch():void{
this.dispatchEvent(new Event("testEvent"));
}
}
}
// Main class code:
package {
import flash.display.Sprite;
public class EventsDemo extends Sprite{
private var listener2:ListenerObject;
public function EventsDemo(){
var dispatcher1:DispatcherObject = new DispatcherObject("D1");
var listener1:ListenerObject = new ListenerObject("L1");
listener1.addDispatcher(dispatcher1);
this.listener2= new ListenerObject("L2");
listener2.addDispatcher(dispatcher1);
this.addEventListener("testEvent", onList1);
dispatcher1.dispatch();
}
public function onListL1():void {
trace("onList L1");
}
}
}
But the problem is: At the “EventsDemo” class, I have the “Listener” class. And the function executede when the event is catched by the listener is the “testEventHandler”, wich is inside “Listener” class. What I want: Some object has the “Dispatcher”. When the “Dispatcher” instance fires an event broadcast, my “Listener” object catchs it and, based on Event type, calls a EventsDemo´s method.
In my particular case, I have a class wich loads an XML and parsing the data. The main class instantiate this XML class. What I want is when the XML have been parsed (some like “onDataDone” event) the main class is notified by a event broadcasting.
It used to be very simple in AS2, but with AS3 I´m going nuts!
Thanks for any help.
pp