zoukankan      html  css  js  c++  java
  • 魔法色块

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

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

    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源代码

  • 相关阅读:
    没人比程序猿更讨厌软件
    随心所欲~我也做个集合遍历器吧(自己的foreach,委托的威力)
    EF架构~DefaultValue让我的UnitOfWork更可读
    EF架构~数据分批批量提交
    陷阱~EF中的Update与Insert共用一个数据上下文
    我心中的核心组件(可插拔的AOP)~第四回 异常拦截器
    zigbee学习:示例程序SampleApp中通讯流程
    【网络可靠版】Extjs4 Treegrid 使用实例
    C#实现微信公众号群发消息(解决一天只能发一次的限制)
    oracle12c(oracle12.1.0.1.0)安装指南--实测OEL5.9(RH5)
  • 原文地址:https://www.cnblogs.com/sevenmoons/p/2507794.html
Copyright © 2011-2022 走看看