as3 页游中,新手指导中,屏蔽所有交互对象,但除了指定交互对象可用的方法

本文介绍了一种用于网页游戏新手引导的交互解决方案,通过锁定除特定目标外的所有鼠标事件来引导玩家完成教程。该方法避免了遮罩层的使用,提供更流畅的游戏体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

玩过几款页游,他们处理新手指导的过程中;

会有一些强制玩家必须点击指导流程所激活的按钮,才可以继续游戏的方法,其实有那么几种:


1)、有些将黑色半透明的显示对象放最顶上,只露出要操作按钮的矩形位置可操作;

2)、有些是将黑色半透明的显示对象放最顶,所有都遮挡;然后将指定的操作对象放到最顶上,操作后,再放回原来的层;

3)、就是目前我推荐的方式:就是,通过,屏蔽掉所有交互对象的,捕获阶段的事件;除了:指定的交互象对


源码如下:

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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值