魔法色块

   本来是想写个祖玛寻宝的,结果写来写去成这样了。

   点击颜色相同的色块相连超过三个的图片即可消除,换新的色块。

package 
{
	import adobe.utils.CustomActions;
	import flash.display.MovieClip;
	import flash.events.MouseEvent ;
	import flash.events.Event;
	import Block ;
	import BlockId;
	import flash.utils.Timer;
	import flash.events .TimerEvent;
	/**
	 * ...
	 * @author ...
	 */
	public class  Game extends MovieClip 
	{
		var block:MovieClip ;
		var MARGINX:int = 90;
		var MARGINY:int = 90;
		private var maparr:Array = [[], [], [], [], [], [], []] ;
		
		private var lockmap:Array = new Array ();
        private var newarr:Array ;
		private var main:Block;
		private var arr:Array ;
		private var maxr:int=7;
		private var maxc:int = 7;
		
		private var timer:Timer;
		public function Game()
		{
			init();
		}
		private function init() //初始化界面
		{
			for (var i:int = 0; i < maxr; i++ )
			{
				for (var j:int = 0; j < maxc ; j++ )
				{
					var id:int = Math.floor(Math.random() * 7 + 1);
					var block:Block = BlockId. creatId(id);
					block .x = MARGINX + j * 32;
					block .y = MARGINY + i * 32;
					block .i = i;
					block .j = j;
					maparr[i][j]= block ;
					addChild (maparr[i][j]);
					block.addEventListener (MouseEvent .CLICK , onclick);
					
				}
			}
		}
		private function onclick(evt:MouseEvent ):void 
		{
			timer = new Timer (300, 1);
			timer.addEventListener(TimerEvent.TIMER, ontimer);
			timer.start ();
			var id:int = evt.currentTarget .id;
			var i:int = evt.currentTarget .i;
			var j:int = evt.currentTarget .j;
			evt.currentTarget.islock = true;
			var arr:Array = [i, j];
	        lockmap.push(arr);
			check(id, i, j);
		}
		function ontimer(e:TimerEvent):void //此时已经寻找到要处理的色块了
		{
			if (lockmap.length >= 3)
			{
				
				clear ();
				timer .removeEventListener (TimerEvent .TIMER , ontimer );
			}
			else 
			{
				lockmap.splice(0);
			    for (var i:int  = 0; i < maxr; i++ )
			    {
			    	for (var j:int = 0; j < maxc; j++ )
				    {
					   maparr[i][j].islock = false;
				    }
			    }
				timer .removeEventListener (TimerEvent .TIMER , ontimer );
			}
		}
		private function check(id:int,i:int,j:int  ):void //搜寻色块
		{
			var ix:int = i;
			var iy:int = j;
			
				addUnlocklist (chectkId (ix+1,iy-1,id));
				addUnlocklist (chectkId (ix+1,iy ,id));
				addUnlocklist (chectkId (ix+1,iy+1,id));
				addUnlocklist (chectkId (ix,iy+1 ,id));
				addUnlocklist (chectkId (ix-1,iy+1 ,id ));
				addUnlocklist (chectkId (ix-1,iy ,id ));
				addUnlocklist (chectkId (ix-1,iy-1,id  ));
				addUnlocklist (chectkId (ix, iy - 1, id ));
			  //  addUnlocklist (chectkId (ix, iy, id ));
		}
		private function addUnlocklist( bll:Boolean)
		{
			//if(bll)
			//trace (lockmap.length )
		}
		private function chectkId(i:int, j:int, idd:int  ):Boolean
		{
		
			//越界
			if (i < 0 || j < 0 || i >= maxr || j >= maxc )
			{
				return false ;
			}
			//加入了lock已经扫描过
		    
			if (maparr [i][j].islock)
			{
				return false;
			}
			if (maparr [i][j].id == idd )
			{
				arr = new Array (i, j);
			    
				maparr [i][j].islock = true ;
				lockmap.push(arr);
				check(idd, i, j);        //以此为中心,再次搜寻
			    
				return false;
			}
			else 
			{
				maparr[i][j].islock = true;
				return false;
			}
		
			return false ;
		}
		private function clear():void 
		{
			addEventListener (Event.ENTER_FRAME , onevent);
			function onevent(e:Event ):void 
			{
				for (var i:int = 0; i < lockmap.length ; i++ )
				{
					var r:int = lockmap [i][0];
					var c:int = lockmap [i][1];
					maparr [r][c].alpha -= 0.1;
					if (maparr [r][c].alpha <= 0.1)
					{
						for (var j:int = 0; j < lockmap.length ; j++ )
						{
				        	var n:int = lockmap [j][0];
				        	var m:int = lockmap [j][1];
						removeChild (maparr [n][m]);
						var id:int = Math.floor(Math.random() * 7 + 1);
					    var block:Block = BlockId. creatId(id);
						block .i = maparr [n][m].i;
						block .j = maparr [n][m].j;
						block.x = MARGINX + maparr [n][m].j * 32;
						block.y = MARGINY + maparr [n][m].i * 32;
						maparr [n][m] = block ;
						addChild (block );
						block.addEventListener (MouseEvent .CLICK , onclick);
						}
						removeEventListener(Event.ENTER_FRAME , onevent );
						lockmap .splice (0);
					}
				}
				
			}
			
			for (var i:int  = 0; i < maxr; i++ )
			{
				for (var j:int = 0; j < maxc; j++ )
				{
					
					maparr[i][j].islock = false;
				}
			}
		
		}
		
	}
	
}

 

Magicblock源代码

posted @ 2012-05-18 15:05  sevenmoons  阅读(216)  评论(0编辑  收藏  举报