在AS3页游中,快捷菜单组件的封装使用

良好的重用;决定良好的封装;

游戏中比较多的一些:

物品、或是聊天框中的链接、选中人物后,左上角出现的人物信息框,等等这些拥有点击弹出菜单功能的地方实现功能都可以按我封装的这个MenuContext实现;

下面简单写了一个:MenuContext,(C#叫:ContextMenu,我故意搞反了)

虽然现在市场上很多页游都有这个功能,他们也应该封装得挺好的,不过我之前却实见过,潦潦草草的实现方式;

不注重重用,所以我这边还是按自己对以前使用C#的WINFORM项目时使用的ContextMenu的封装方式来实现;


源码项目:MenuManagerTestingProject


DEMO 效果图:



外部调用时,主要的代码:

package
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import view.ScaleBitmap;
	
	
	/**
	 * 测试快捷菜单类
	 * @author Jave.Lin
	 * @date 2013-9-18
	 **/
	[SWF(width="1000", height="600")]
	public class MenuManagerTestingProject extends Sprite
	{
		[Embed(source="../res/S9G1_10_10_40_40.png")]
		public static var S9D1_10_10_40_40:Class;
		
		public function MenuManagerTestingProject()
		{
			stage.frameRate = 60;
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.color = 0xeeeeee;
			var self:MenuManagerTestingProject = this;
			
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			function onAddedToStage(e1:Event):void
			{
				var menuItemDataArr:Array = [
					{"name":"使用", "key":"use"},
					{"name":"装备", "key":"install"},
					{"name":"丢弃", "key":"discard"},
					{"name":"测试菜单项好长啊,怎么回事", "key":"testing"},
					{"name":"测试菜单项好长啊,怎么回事", "key":"testing"},
					{"name":"菜单项是可以重复的!嘿嘿!", "key":"testing"},
				];
				var menuItemDataArr1:Array = [
					{"name":"1", "key":"menu1"},
					{"name":"2", "key":"menu2"},
					{"name":"3", "key":"menu3"},
					{"name":"测试", "key":"menu testing"},
					{"name":"other", "key":"menu other"},
					{"name":"◆☆★", "key":"menu special chars", "enabled":false},
				];
				//方法一:自定义怎么用都可以
				var sp:Sprite = new Sprite();
				sp.name = "绿色矩形";
				sp.graphics.clear();
				sp.graphics.beginFill(0x00ff00);
				sp.graphics.drawRect(0, 0, 50, 50);
				sp.graphics.endFill();
				sp.x = 100;
				sp.y = 100;
				
				var sp1:Sprite = new Sprite();
				sp1.name = "蓝色矩形";
				sp1.graphics.clear();
				sp1.graphics.beginFill(0x00ffff);
				sp1.graphics.drawRect(0, 0, 50, 50);
				sp1.graphics.endFill();
				sp1.x = 200;
				sp1.y = 100;
				
				self.parent.addChild(sp);
				sp.addEventListener(MouseEvent.CLICK, onClick);
				
				self.parent.addChild(sp1);
				sp1.addEventListener(MouseEvent.CLICK, onClick);
				
				var s9gBg:ScaleBitmap = new ScaleBitmap((new S9D1_10_10_40_40() as Bitmap).bitmapData);
				var s9gRect:Rectangle = new Rectangle(20, 20, 10, 10);
				s9gBg.scale9Grid = s9gRect;
				
				var menuContext:MenuContext = new MenuContext(s9gBg);
				menuContext.menuDataArr = menuItemDataArr;
				menuContext.showOn(self.parent, 300, 100);
				menuContext.addEventListener(MenuContextEvent.SELECT, onMenuSelect);
				
				function onMenuSelect(e:MenuContextEvent):void
				{
					var tag:String = e.selectedItem.menuContext.tag ? e.selectedItem.menuContext.tag.name : "没有选中对象时,";
					trace("在" + tag + "选中菜单:" +  e.selectedItem);
				}
				
				function onClick(e:MouseEvent):void
				{
					var menuData:Array;
					var sprite:Sprite = e.currentTarget as Sprite;
					menuData = sprite == sp ? menuItemDataArr : menuItemDataArr1;
					menuContext.tag = sprite;
					menuContext.menuDataArr = menuData;
					menuContext.showOn(self.parent, e.stageX, e.stageY);
					trace("shown");
				}
				
				//方法二:使用MenuContextManager统一使用
				var s1:Sprite1 = new Sprite1();
				addChild(s1);
				s1.x = 100;
				s1.y = 200;
				
				var s2:Sprite2 = new Sprite2();
				addChild(s2);
				s2.x = 200;
				s2.y = 200;
				
				s1.menuContext = menuContext;
				s2.menuContext = menuContext;
				
				MenuContextManager.getInstance().registerStage(stage);
				
				MenuContextManager.getInstance().register(s1);
				MenuContextManager.getInstance().register(s2);
			}
		}
	}
}
import flash.display.Sprite;

class Sprite1 extends Sprite implements IMenuContextAble
{
	private var menuItemDataArr1:Array;
	private var _menuContext:IMenuContext;
	
	public function Sprite1()
	{
		name = "Sprite1蓝色矩形";
		graphics.clear();
		graphics.beginFill(0x0000ff);
		graphics.drawRect(0, 0, 50, 50);
		graphics.endFill();
		
		menuItemDataArr1 = [
			{"name":"1", "key":"menu1", "enabled":false},
			{"name":"2", "key":"menu2"},
			{"name":"3", "key":"menu3", "enabled":false},
			{"name":"测试", "key":"menu testing"},
			{"name":"other", "key":"menu other", "enabled":false},
			{"name":"◆☆★", "key":"menu special chars", "enabled":false},
		];
	}
	
	public function getMenuDataArr():Array
	{
		// TODO Auto Generated method stub
		return menuItemDataArr1;
	}
	
	public function get menuContext():IMenuContext
	{
		// TODO Auto Generated method stub
		return _menuContext;
	}
	
	public function set menuContext(value:IMenuContext):void
	{
		// TODO Auto Generated method stub
		_menuContext = value;
	}
}

class Sprite2 extends Sprite implements IMenuContextAble
{
	private var menuItemDataArr:Array;
	private var _menuContext:IMenuContext;
	
	public function Sprite2()
	{
		name = "Sprite2黄色矩形";
		graphics.clear();
		graphics.beginFill(0xffff00);
		graphics.drawRect(0, 0, 50, 50);
		graphics.endFill();
		
		menuItemDataArr = [
			{"name":"使用", "key":"use"},
			{"name":"装备", "key":"install"},
			{"name":"丢弃", "key":"discard"},
			{"name":"测试菜单项好长啊,怎么回事", "key":"testing"},
			{"name":"测试菜单项好长啊,怎么回事", "key":"testing"},
			{"name":"菜单项是可以重复的!嘿嘿!", "key":"testing"},
		];
	}
	
	public function getMenuDataArr():Array
	{
		// TODO Auto Generated method stub
		return menuItemDataArr;
	}
	
	public function get menuContext():IMenuContext
	{
		// TODO Auto Generated method stub
		return _menuContext;
	}
	
	public function set menuContext(value:IMenuContext):void
	{
		// TODO Auto Generated method stub
		_menuContext = value;
	}
}


你可能感兴趣的:(在AS3页游中,快捷菜单组件的封装使用)