Skip to content

Instantly share code, notes, and snippets.

@lynxerzhang
Created June 2, 2013 07:33
Show Gist options
  • Save lynxerzhang/5692924 to your computer and use it in GitHub Desktop.
Save lynxerzhang/5692924 to your computer and use it in GitHub Desktop.
package
{
import flash.display.DisplayObject;
import flash.events.MouseEvent;
/**
* @see http://blog.iconara.net/2008/03/30/separating-event-handling-from-event-filtering/
*
* @example
* container is a Sprite Container, childA and childB and container's children
* clickChildA is childA's event handler
* clickChildB is childB's event handler
*
* container.addEventListener(MouseEvent.CLICK, Guarded.guard(clickChildA, Guarded.mouseTargetGuide(MouseEvent.CLICK, childA)));
* container.addEventListener(MouseEvent.CLICK, Guarded.guard(clickChildB, Guarded.mouseTargetGuide(MouseEvent.CLICK, childB)));
*
* function clickChildA(evt:MouseEvent):void{
* trace("childA clicked");
* }
*
* function clickChildB(evt:MouseEvent):void{
* trace("childB clicked");
* }
*/
public class Guarded
{
public static function guard(run:Function, gd:Function):Function {
return function(...args):void {
if (gd.apply(null, args)) {
run.apply(null, args);
}
}
}
public static function mouseTypeGuide(mouseName:String):Function {
return function(evt:MouseEvent):Boolean {
return evt.type == mouseName;
}
}
public static function mouseTargetGuide(mouseName:String, target:DisplayObject):Function {
return function(evt:MouseEvent):Boolean {
return evt.type == mouseName && evt.target == target;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment