package { import flash.display.InteractiveObject; import flash.display.Stage; import flash.events.MouseEvent; /** * 新手指导管理器 * @author jave.lin * @date 2013-7-24 */ public class GuideManager{ private static var stage:Stage; /**设置舞台*/ public static function setStage(stage:Stage):void{ GuideManager.stage = stage; } /**锁定全局*/ public static function lockAll():void{ if(!stage) throw new Error("GuideManager未设置stage"); stage.addEventListener(MouseEvent.CLICK, onLockAll, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_DOWN, onLockAll, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_UP, onLockAll, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_MOVE, onLockAll, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_OVER, onLockAll, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_OUT, onLockAll, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_WHEEL, onLockAll, true, int.MAX_VALUE); } /**解除锁定全局*/ public static function unLockAll():void{ stage.removeEventListener(MouseEvent.CLICK, onLockAll, true); stage.removeEventListener(MouseEvent.MOUSE_DOWN, onLockAll, true); stage.removeEventListener(MouseEvent.MOUSE_UP, onLockAll, true); stage.removeEventListener(MouseEvent.MOUSE_MOVE, onLockAll, true); stage.removeEventListener(MouseEvent.MOUSE_OVER, onLockAll, true); stage.removeEventListener(MouseEvent.MOUSE_OUT, onLockAll, true); stage.removeEventListener(MouseEvent.MOUSE_WHEEL, onLockAll, true); } private static function onLockAll(e:MouseEvent):void{ e.preventDefault(); e.stopImmediatePropagation(); e.stopPropagation(); } /**当前激活,可以控制的对象*/ public static var curActivedObj:InteractiveObject; /** * 屏蔽掉所有鼠标操作,但除了指定的obj交互对象 * (如果需要屏蔽键盘操作也但样加上对所有键盘事件的处理) * */ public static function lockAllButThisOne(obj:InteractiveObject):void{ unLock(); curActivedObj = obj; stage.addEventListener(MouseEvent.CLICK, checkEvent, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_DOWN, checkEvent, true, int.MAX_VALUE); stagej.addEventListener(MouseEvent.MOUSE_UP, checkEvent, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_MOVE, checkEvent, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_OVER, checkEvent, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_OUT, checkEvent, true, int.MAX_VALUE); stage.addEventListener(MouseEvent.MOUSE_WHEEL, checkEvent, true, int.MAX_VALUE); } /**解除屏蔽*/ public static function unLock():void{ if(stage){ stage.removeEventListener(MouseEvent.CLICK, checkEvent, true); stage.removeEventListener(MouseEvent.MOUSE_DOWN, checkEvent, true); stage.removeEventListener(MouseEvent.MOUSE_UP, checkEvent, true); stage.removeEventListener(MouseEvent.MOUSE_MOVE, checkEvent, true); stage.removeEventListener(MouseEvent.MOUSE_OVER, checkEvent, true); stage.removeEventListener(MouseEvent.MOUSE_OUT, checkEvent, true); stage.removeEventListener(MouseEvent.MOUSE_WHEEL, checkEvent, true); } } /**检查、滤过交互对象的事件*/ private static function checkEvent(e:MouseEvent):void{ if(e.target != curActivedObj){//所有鼠标触发的事件都屏蔽 e.preventDefault(); e.stopImmediatePropagation(); e.stopPropagation(); } } } }
import flash.display.Sprite; class Main extends Sprite{ private var btnVec:Vector.<Sprite>; public function Main(){ btnVec = new Vector.<Sprite>(); for (var i:int = 0; i < 10; i++){ var btn:Sprite = getBtn(); btn.x = 100; btn.y = 100 + (30 * i); addChild(btn); } //这里我只想第5个按钮可用,其它都不可用即可 GuideManager.setStage(stage);//这里只需要在游戏初始化时setStage一次即可 GuideManager.lockAllButThisOne(btnVec[4]); // //解除屏蔽 // GuideManager.unLock(); } private function getBtn():Sprite{ var result:Sprite = new Sprite(); result.graphics.beginFill(uint(Math.random() * uint.MAX_VALUE)); result.graphics.drawRect(0, 0, 100, 30); result.graphics.endFill(); return result; } }