LIVE CODING 一時間で箱入り娘作る。 on 2010-04-05

by shohei909
♥1 | Line 130 | Modified 2010-04-06 16:27:16 | MIT License
play

ActionScript3 source code

/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/toGl
 */

<?xml version="1.0" encoding="utf-8"?>
<!--
LIVE CODING 今から1時間で箱入り娘作る。 2010/04/05 22:50~0:20
今日寝る前にパズルゲームの箱入り娘作る。



ルール
クリック&ドラッグで赤いのを動かして、GOALまで運んでください。




一時間半もかかってしまった・・・







2010/04/06
デバッグ、動作確認、操作性の向上、見た目やエフェクトの調整、などしました。
こっちの方がたぶん時間かかってる・・・。
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	applicationComplete="init()" backgroundColor="0x222222">
		<mx:Move id="moveE" duration="60" />
		<mx:Parallel id="clearE">
            <mx:Blur duration="200" /> 
            <mx:Fade duration="100" /> 
	    </mx:Parallel>

		
		<mx:Canvas id="field" backgroundColor="0xFFFFFF" width="203" height="253" cornerRadius="5" borderStyle="solid">
	    		<mx:Text text="GOAL" x="70" y="220" fontSize="20" color="0xFF9977"/>
		</mx:Canvas>
	    <mx:Text id="trace" resizeEffect="clearE" color="0xFF2200" fontSize="40"/>
	    	<mx:Script>
        		<![CDATA[
        			import mx.core.Container;
        			import flash.events.MouseEvent;
        			
        			public var map:Vector.<Vector.<int>>; 
        			
        			public var block:Vector.<Object>;
        			
        			
        			public function init():void{
        				map=new Vector.<Vector.<int>>(4);
        				for(var i:int=0;i<4;i++){
        					map[i]=new Vector.<int>(4);
        					for(var j:int=0;j<5;j++){
        						map[i][j] = -1;
        					}
        				}
        				
        				block = new Vector.<Object>(10);
        				block[0]={w:2,h:2,x:1,y:0,cont:new Container(),name:"娘"};
        				block[1]={w:1,h:2,x:0,y:0,cont:new Container(),name:"父親"};
        				block[2]={w:1,h:2,x:3,y:0,cont:new Container(),name:"母親"};
        				block[3]={w:1,h:2,x:0,y:2,cont:new Container(),name:"祖父"};
        				block[4]={w:1,h:2,x:3,y:2,cont:new Container(),name:"祖母"};
        				block[5]={w:2,h:1,x:1,y:2,cont:new Container(),name:"兄弟"};
        				block[6]={w:1,h:1,x:1,y:3,cont:new Container(),name:"子"};
        				block[7]={w:1,h:1,x:2,y:3,cont:new Container(),name:"子"};
        				block[8]={w:1,h:1,x:0,y:4,cont:new Container(),name:"子"};
        				block[9]={w:1,h:1,x:3,y:4,cont:new Container(),name:"子"};
        				
        				for(i=0;i<10;i++){
        					block[i].cont.setStyle("borderStyle","solid");
        					block[i].cont.setStyle("cornerRadius",5)
        					block[i].cont.setStyle("moveEffect","moveE");
        					block[i].cont.setStyle("backgroundColor",0xFFDDCC);
        					block[i].cont.x = 50*block[i].x;
        					block[i].cont.y = 50*block[i].y;
        					block[i].cont.width = 50*block[i].w+1;
        					block[i].cont.height = 50*block[i].h+1;
        					field.addChild(block[i].cont);
        					
        					for(var ax:int=0;ax<block[i].w;ax++){
        						for(var ay:int=0;ay<block[i].h;ay++){
        							map[ax+block[i].x][ay+block[i].y] = i;
        						}
        					}
        				}
        				block[0].cont.setStyle("backgroundColor",0xFFBBAA);
        					
        				field.addEventListener("mouseDown",down);
        				stage.addEventListener("mouseUp",up);
        				addEventListener("enterFrame",frame);
        				field.mouseChildren=false;
        				
        				moveE.addEventListener("tweenStart",onTweenStart);
        				moveE.addEventListener("tweenEnd",onTweenEnd);
        			}
        			
        			var downX:int = 0; var downY:int = 0;
        			var d:Boolean = false;
        			var select:int = 0;
        			public function down(e:MouseEvent){
        				downX=e.localX;downY=e.localY;
        				select = map[Math.floor(downX/50)][Math.floor(downY/50)];
        				d = true;
        			}
        			public function up(e:MouseEvent){
        				d = false;
        			}
        			
        			public function frame(e:Event){
        				if(d && tween == false){
        					if(downX+40 < field.mouseX && moveB(select,1,0)){downX+=40;}
        					else if(downX-40 > field.mouseX && moveB(select,-1,0)){downX-=40;}
        					else if(downY+40 < field.mouseY && moveB(select,0,1)){downY+=40;}
        					else if(downY-40 > field.mouseY && moveB(select,0,-1)){downY-=40;}
        				}
        			}
        			public function moveB(s:int,dx:int,dy:int):Boolean{
        				var movable:Boolean = true;
        				for(var ax:int=0;ax<block[s].w;ax++){
        					for(var ay:int=0;ay<block[s].h;ay++){
        						var nx:int=ax+block[s].x+dx;var ny:int=ay+block[s].y+dy;
        						if( nx>-1 && ny>-1 && nx<4 && ny<5 &&
        								(map[nx][ny] == s || map[nx][ny] == -1) ){
        						}else{
        							movable =false;
        						}
        					}
        				}
        				if(movable){
        					for(var ax:int=0;ax<block[s].w;ax++){
	        					for(var ay:int=0;ay<block[s].h;ay++){
	        						var nx:int=ax+block[s].x;var ny:int=ay+block[s].y;
	        						map[nx][ny] = -1
	        					}
        					}
        					
        					for(var ax:int=0;ax<block[s].w;ax++){
	        					for(var ay:int=0;ay<block[s].h;ay++){
	        						var nx:int=ax+block[s].x+dx;var ny:int=ay+block[s].y+dy;
	        						map[nx][ny] = s;
	        					}
        					}
        					block[s].cont.x += dx*50;
        					block[s].cont.y += dy*50;
        					block[s].x += dx;
        					block[s].y += dy;
        					
        					if(block[0].x==1 && block[0].y==3){trace.text="CLEAR!";}
        				}
        				return movable;
        			}
        			
        			public var tween:Boolean=false;
        			public function onTweenEnd(e:Event):void{tween=false;}
        			public function onTweenStart(e:Event):void{tween=true;}
	        ]]>
	        
	    </mx:Script>
</mx:Application>