Created
June 2, 2013 07:33
-
-
Save lynxerzhang/5692924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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