Skip to content

Instantly share code, notes, and snippets.

@myobie
Created April 21, 2009 19:25
Show Gist options
  • Save myobie/99328 to your computer and use it in GitHub Desktop.
Save myobie/99328 to your computer and use it in GitHub Desktop.
// CustomEvent.as
package com.utils {
import flash.events.Event;
public class CustomEvent extends Event {
// event constants
public static const EVENT_NAME:String = "eventName";
public static const OTHER_EVENT:String = "otherEvent";
public function CustomEvent($type:String, $bubbles:Boolean = false, $cancelable:Boolean = false) {
super($type, $bubbles, $cancelable);
}
// Adobe recommend overriding this
public override function clone():Event {
return new CustomEvent(type, this.params, bubbles, cancelable);
}
}
}
// Usage:
// ## Main.as
import com.utils.CustomEvent;
public function fired_event(e:CustomEvent) {
trace("Event fired from child");
}
cropper_box.addEventListener(CustomEvent.EVENT_NAME, fired_event);
// ## CropperBox.as
import com.utils.CustomEvent;
dispatchEvent(new CustomEvent(CustomEvent.EVENT_NAME));
/* ----- If this doesn't work ----- */
// Central.as
package com.utils {
import flash.events.*;
public class Central {
protected static var disp:EventDispatcher;
// event constants
public static const EVENT_NAME:String = "eventName";
public static const OTHER_EVENT:String = "otherEvent";
public static function dispatchEvent(p_event:Event):void {
if (disp == null) { return; }
disp.dispatchEvent(p_event);
}
public static function addEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false, p_priority:int=0, p_useWeakReference:Boolean=false):void {
if (disp == null) { disp = new EventDispatcher(); }
disp.addEventListener(p_type, p_listener, p_useCapture, p_priority, p_useWeakReference);
}
public static function removeEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false):void {
if (disp == null) { return; }
disp.removeEventListener(p_type, p_listener, p_useCapture);
}
}
}
// Usage:
// ## Main.as
import com.utils.Central;
public function fired_event(e:Event) {
trace("Event fired from child");
}
Central.addEventListener(Central.EVENT_NAME, fired_event);
// ## CropperBox.as
import com.utils.Central;
dispatchEvent(new Event(Central.EVENT_NAME));
/* or maybe a combo of them both */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment