Copy link to clipboard
Copied
I've got 3 classes:
The class that recognizes the change in URL (using SWFAddress by Asual):
package com.zeeto.swfaddress {
import flash.display.MovieClip;
import com.asual.swfaddress.*;
public class SwfAddress extends MovieClip {
private var dispatcher:Dispatch = new Dispatch;
public function SwfAddress():void {
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, onChange);
}
private function onChange(e:SWFAddressEvent):void {
dispatcher.changed();
}
}
}
The class "Dispatch" that validates the URL and dispatching Event when finished
package com.zeeto.swfaddress {import flash.events.Event;
import flash.events.EventDispatcher;
public class Dispatch extends EventDispatcher {
public static const CHANGED:String = "changed";
public function changed ():void {
// some operations validating the URL
dispatchEvent(new Event(Dispatch.CHANGED));
}
}
}
Other class in other package that should receive info when the validation process has finished.
package com.zeeto.menu {import com.zeeto.swfaddress.*
public class MenuPanel extends MovieClip {
var swfRead:Dispatch = new Dispatch;
public function MenuPanel():void {
swfRead.addEventListener(Dispatch.CHANGED, onChange);
}
private function onChange(e:Event):void {
trace("Hello World");
}
}
And the "Hello World" never appeared in the output window - so I'm not sure if is it possible that my MenuPanel has a chance to receive a info about completing the validation triggered by some other class?
Copy link to clipboard
Copied
Your MenuPanel instance should be listening to the "changed" event from Dispatch instance "dispatcher". As it is you are listening the event from "swfRead".
--
Kenneth Kawamoto
Copy link to clipboard
Copied
You could use a event proxy that will be a static class..that everything in your solution would have access to.
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
public class ProxyDispatcher
{
public static var dispatcher:EventDispatcher = new EventDispatcher();
public function ProxyDispatcher():void {
throw new Error("ProxyDispatcher is a static class. Do not instantiate.");
}
public static function addEventListener(i_type:String, i_listener:Function, i_useCapture:Boolean = false, i_priority:int = 0, i_useWeakReference:Boolean = false):void {
dispatcher.addEventListener(i_type, i_listener, i_useCapture, i_priority, i_useWeakReference);
}
public static function removeEventListener(i_type:String, i_listener:Function, i_useCapture:Boolean = false):void {
dispatcher.removeEventListener(i_type, i_listener, i_useCapture);
}
public static function hasEventListener(i_type:String):Boolean {
return dispatcher.hasEventListener(i_type)
}
public static function dispatchEvent(i_event:Event):void {
dispatcher.dispatchEvent(i_event);
}
}
}
Then in your document class you would handle all events thru this proxy through static reference.
public function SwfAddress():void {
ProxyDispatcher.addEventListener(SWFAddressEvent.CHANGE, onChange);
}
you would than setup listeners for that event on whatever class, in whatever scope you need. and fire functions accordingly.
This way theres only once instance of your dispatcher running across your project (singleton).