ActionScript 3.0 学习(十二) AS3 模拟打地鼠小游戏

        有问题请联系qq:1031885567,注明csdn加好友。 

        本节做了一个模拟打地鼠小游戏,可以学习其中将各个方格用统一的类来管理,同时需要注意的是在将子原件移除掉时,也相应需要将其在数组中splice掉。以下为对应程序:

        

package
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ItemManage extends Sprite
	{
		private var _container:MovieClip;
		private var _circleEffect:MovieClip;
		private var _rightMark:MovieClip;
		
		private var _itemIcons:Vector.;
		private var _nameArray:Array = ["item_0", "item_2", "item_5", "item_4", "item_6",
			"item_10", "item_8", "item_7", "item_3", "item_11", 
			"item_12", "item_9", "item_14", "item_16", "item_17",
			"item_13", "item_23", "item_24", "item_18", "item_19",
			"item_22", "item_20", "item_15", "item_21", "item_1"];
		
		public function ItemManage()
		{
			_container = new ItemContainerUI();
			_circleEffect = new circleUI();
			_rightMark = new DuiGouUI();
			
			_itemIcons = new Vector.;
			
			var length:int = _container.numChildren;
			for(var i:int = 0; i < length; i++)
			{
				var itemIconMC:MovieClip = _container.getChildByName(_nameArray[i]) as MovieClip;
				_itemIcons[i] = new ItemIcon(itemIconMC);
				_itemIcons[i].addEventListener(MouseEvent.CLICK, onClickIcon);
			}
			
			_itemIcons[3].selected = true;
			_itemIcons[10].addChildItem(new circleUI(), 33, 33);
			_itemIcons[10].enableMouse(true);
			
			addChild(_container);
		}
		
		private function onClickIcon(e:MouseEvent):void
		{
			var index:int = _nameArray.indexOf(e.currentTarget.skin.name);
			_itemIcons[index].removeChildItem();
			_itemIcons[index].enableMouse(false);
			_itemIcons[index].selected = true;
			var num:int = Math.floor(Math.random() * 25);
			_itemIcons[num].addChildItem(new circleUI(), 33, 33);
			_itemIcons[num].enableMouse(true);
		}
	}
}


import flash.display.MovieClip;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;

class ItemIcon extends EventDispatcher
{
	private var _skin:MovieClip;
	private var _selected:Boolean;
	private var _selectedMC:MovieClip;
	private var _selectedMCName:String;
	private var _addedChild:Vector.;
	
	public function ItemIcon(skin:MovieClip)
	{
		_skin = skin;
		_skin.mouseEnabled = false;
		_skin.buttonMode = false;
		_addedChild = new Vector.;
	}
	
	public function get skin():MovieClip
	{
		return _skin;
	}
	
	public function set selected(value:Boolean):void
	{
		_selected = value;
		if(value)
		{
			if(_selectedMC == null)
			{
				_selectedMC = new DuiGouUI();
				_selectedMCName = _selectedMC.name;
				_selectedMC.x = _skin.width / 2;
				_selectedMC.y = _skin.height /2;
				_skin.addChild(_selectedMC);
			}
		}
		else
		{
			if(_selectedMC)
			{
				_skin.removeChild(_selectedMC);
				_selectedMC = null;
			}
		}
	}
	
	public function enableMouse(value:Boolean):void
	{
		if(value)
		{
			_skin.buttonMode = true;
			_skin.mouseEnabled = true;
			_skin.addEventListener(MouseEvent.CLICK, onClick);
		}
		else
		{
			_skin.mouseEnabled = false;
			_skin.buttonMode = false;
			if(_skin.hasEventListener(MouseEvent.CLICK))
			{
				_skin.removeEventListener(MouseEvent.CLICK, onClick);
			}
		}
	}
	
	public function addChildItem(mc:MovieClip, x:int ,y:int):void
	{
		mc.x = x;
		mc.y = y;
		_skin.addChild(mc);
		_addedChild.push(mc);
	}
	
	public function removeChildItem():void
	{
		for(var i:int = 0; i < _addedChild.length; i++)
		{
			if(_addedChild[i].name != _selectedMCName)
			{
				_skin.removeChild(_addedChild[i]);
				_addedChild.splice(i,1);
				i--;
			}
		}
	}
	
	private function onClick(event:MouseEvent):void
	{
		dispatchEvent(new MouseEvent(MouseEvent.CLICK));
	}
	
	public function dispose():void
	{
		_skin = null;
		_selectedMC = null;
		_addedChild = null;
		if(_skin.hasEventListener(MouseEvent.CLICK))
		{
			_skin.removeEventListener(MouseEvent.CLICK, onClick);
		}
	}
}

以下为运行结果:

ActionScript 3.0 学习(十二) AS3 模拟打地鼠小游戏_第1张图片

你可能感兴趣的:(ActionScript,3.0)