In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ICU-Fight

Get Adobe Flash player
by twotree 31 May 2010
/**
 * Copyright twotree ( http://wonderfl.net/user/twotree )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lJ1r
 */

package {
    import flash.display.*;
    import flash.events.*;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    import Box2D.Dynamics.b2World;
    import Box2D.Dynamics.b2DebugDraw;
    import Box2D.Collision.b2AABB;
    import Box2D.Dynamics.b2Body;
    import Box2D.Common.Math.b2Vec2;
    import flash.net.URLRequest;
    import Box2D.Dynamics.Joints.*;
    import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	import flash.filters.DropShadowFilter;

    
    [SWF(backgroudColor="0x0000FF",width="631",height="624")]
    
    public class main extends Sprite {
    	    		
    		public var myRequest:URLRequest;
    		public var myLoader:Loader;
    		
    		private var world:b2World;
    		private var scale:Number =10;
    		private var hako:Array = [];
    		private var hakoImg:Array = [];
    		private var anc:Array = [];
    	   		
    		//var image:Sprite = new Sprite();
    		
        public function main() {
        		var worldAABB:b2AABB = new b2AABB();//重力が有効な範囲を指定する
        		worldAABB.lowerBound.Set(-100,-100);//上側の有効範囲
        		worldAABB.upperBound.Set(stage.stageWidth + 100, stage.stageHeight + 100);//下側の有効範囲
        		
        		var gravity:b2Vec2 = new b2Vec2(0,50);//重力がかかる方向、重さ
        		world = new b2World(worldAABB, gravity, true);//物理世界のインスタンス化(重力を付けて)
        		
        		var draw:b2DebugDraw = new b2DebugDraw();//オブジェクトの描画をする
        		draw.m_sprite = this;//描画するSPRITEの設定
        		draw.m_drawScale = scale;//一ピクセルの大きさを設定(1メートル何ピクセルなのか)
        		//draw.m_drawFlags= b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;//オブジェクトの輪郭を表示する
        		world.SetDebugDraw(draw);//オブジェクトをセットする
        		
        		Shape.world = world;//Shapeが属するworldの設定
        		Shape.scale = scale;//Shapeのscale
        		 
        		//***************画像の表示************
        		myRequest = new URLRequest("http://lh4.ggpht.com/_7oOZzghoD1U/TAE3EsfgofI/AAAAAAAAAB4/FbNvn8cvnx0/s640/resize32.jpg");
        		myLoader = new Loader();
        		myLoader.load(myRequest);
        		addChild(myLoader);    		
        		//***********************************
        	        		
        		//*************オブジェクトを作る**************************
        		var body:b2Body;
        		//舞台をつくる
        		const butai_h:Number = 50; //土台の高さを決める
        		Shape.create({shape:Shape.RECT, x:0,y:stage.stageHeight - butai_h,width:stage.stageWidth,height:butai_h});
        		//Shape.create({shape:Shape.RECT, x:stage.stageWidth + 5, y:0, width:10, height:stage.stageHeight});
        		//Shape.create({shape:Shape.RECT, x:-15, y:0, width:10, height:stage.stageHeight});
        		
        		//人間のオブジェクトをつくる
        		var he:int = 0;
        		var d:Number=350;
        		var e:Number = 50;
        		/*head,0*/     hako[he++] = Shape.create({shape:Shape.CIRCLE, x:258, y:18+d, radius:17, density:0.5, restitution:0.5,density:100});
        		/*torsoA,1*/   hako[he++] = Shape.create({shape:Shape.RECT, x:232, y:35+d, width:53, height:28, restitution:0,density:100} );
			/*torsoB,2*/   hako[he++] = Shape.create({shape:Shape.RECT, x:236, y:57+d, width:45, height:28, restitution:0,density:100 } );
			/*torsoC,3*/   hako[he++]= Shape.create( {shape:Shape.RECT, x:238, y:77+d, width:40, height:28, restitution:0,density:100 } );
			/*plvis,4*/    hako[he++] = Shape.create( {shape:Shape.RECT, x:236, y:97+d, width:45, height:25, restitution:0,density:100 } );
			/*leftArm_U,5*/	 hako[he++] = Shape.create( {shape:Shape.RECT, x:185, y:43+d, width:50, height:15, restitution:0,density:10 } );
			/*leftArm_S,6*/	 hako[he++] = Shape.create( {shape:Shape.RECT, x:135, y:43+d, width:50, height:15, restitution:0,density:10 } );
			/*rightArm_U,7*/ hako[he++] = Shape.create( {shape:Shape.RECT, x:280, y:43+d, width:50, height:15, restitution:0,density:10 } );
			/*rightArm_S,8*/ hako[he++] = Shape.create( {shape:Shape.RECT, x:330, y:43+d, width:50, height:15, restitution:0,density:10 });
			/*leftLeg_U,9*/  hako[he++] = Shape.create( {shape:Shape.RECT, x:235, y:123+d, width:16, height:55, restitution:0,density:10  });
			/*rightLeg_U,10*/  hako[he++] = Shape.create( {shape:Shape.RECT, x:264, y:123+d, width:16, height:55, restitution:0,density:10 } );
			/*leftLeg_S,11*/ hako[he++] = Shape.create( {shape:Shape.RECT, x:235, y:176+d, width:16, height:55, restitution:0,density:10 } );
			/*rightLeg_S,12*/ hako[he++] = Shape.create( {shape:Shape.RECT, x:264, y:176+d, width:16, height:55, restitution:0,density:10 } );
			
			/*anchor*/ anc[0] = Shape.create({shape:Shape.CIRCLE,x:258, y:d-e,radius:1});
			
			//人間のイメージを作る
			var i:int = 0;
			cirImg(17,i++);
			recImg(53,28,i++);
			recImg(45,28,i++);
			recImg(40,28,i++);
			recImg(45,25,i++);
			recImg(50,15,i++); 
			recImg(50,15,i++);
			recImg(50,15,i++);
			recImg(50,15,i++);
			recImg(16,55,i++);
			recImg(16,55,i++);
			recImg(16,55,i++);
			recImg(16,55,i++);
			
			//人間のオブジェクトをジョイントする
			jtR(hako[0],hako[1],hako[0].GetWorldCenter().x,hako[0].GetWorldCenter().y,1,-1);
			jtR(hako[1],hako[2],hako[1].GetWorldCenter().x,hako[1].GetWorldCenter().y,1,-1);
			jtR(hako[2],hako[3],hako[2].GetWorldCenter().x,hako[2].GetWorldCenter().y,1,-1);
			jtR(hako[3],hako[4],hako[3].GetWorldCenter().x,hako[3].GetWorldCenter().y,1,-1);
			jtR(hako[1],hako[5],hako[1].GetWorldCenter().x-3,hako[1].GetWorldCenter().y,45,-45);
			jtR(hako[6],hako[5],hako[6].GetWorldCenter().x+2,hako[6].GetWorldCenter().y,45,-45);
			jtR(hako[1],hako[7],hako[1].GetWorldCenter().x+3,hako[1].GetWorldCenter().y,45,-45);
			jtR(hako[8],hako[7],hako[8].GetWorldCenter().x-2,hako[8].GetWorldCenter().y,45,-45);
			jtR(hako[4],hako[9],hako[9].GetWorldCenter().x,hako[9].GetWorldCenter().y-3,45,-45);
			jtR(hako[9],hako[11],hako[11].GetWorldCenter().x,hako[11].GetWorldCenter().y-2,45,-45);
			jtR(hako[4],hako[10],hako[10].GetWorldCenter().x,hako[10].GetWorldCenter().y-3,90,-90);
			jtR(hako[10],hako[12],hako[12].GetWorldCenter().x,hako[12].GetWorldCenter().y-2,90,-90);
			
			jtR(hako[0],anc[0],anc[0].GetWorldCenter().x,anc[0].GetWorldCenter().y,180,-180);
			
			
			const c:Number = 200;
			/*head,13*/     hako[he++] = Shape.create({shape:Shape.CIRCLE, x:258+c, y:18+d, radius:17, density:0.5, restitution:0.5,density:100});
        		/*torsoA,14*/   hako[he++] = Shape.create({shape:Shape.RECT, x:232+c, y:35+d, width:53, height:28, restitution:0.3,density:100 } );
			/*torsoB,15*/   hako[he++] = Shape.create({shape:Shape.RECT, x:236+c, y:57+d, width:45, height:28, restitution:0.3,density:100} );
			/*torsoC,17*/   hako[he++]= Shape.create( {shape:Shape.RECT, x:238+c, y:77+d, width:40, height:28, restitution:0.3,density:100 } );
			/*plvis,18*/    hako[he++] = Shape.create( {shape:Shape.RECT, x:236+c, y:97+d, width:45, height:25, restitution:0.3,density:100} );
			/*leftArm_U,19*/	 hako[he++] = Shape.create( {shape:Shape.RECT, x:185+c, y:43+d, width:50, height:15, restitution:0.3,density:10} );
			/*leftArm_S,20*/	 hako[he++] = Shape.create( {shape:Shape.RECT, x:135+c, y:43+d, width:50, height:15, restitution:0.3,density:10} );
			/*rightArm_U,21*/ hako[he++] = Shape.create( {shape:Shape.RECT, x:280+c, y:43+d, width:50, height:15, restitution:0.3,density:10 } );
			/*rightArm_S,22*/ hako[he++] = Shape.create( {shape:Shape.RECT, x:330+c, y:43+d, width:50, height:15, restitution:0.3,density:10 });
			/*leftLeg_U,23*/  hako[he++] = Shape.create( {shape:Shape.RECT, x:235+c, y:123+d, width:16, height:55, restitution:0.3,density:10 } );
			/*rightLeg_U,24*/  hako[he++] = Shape.create( {shape:Shape.RECT, x:264+c, y:123+d, width:16, height:55, restitution:0.3,density:10} );
			/*leftLeg_S,25*/ hako[he++] = Shape.create( {shape:Shape.RECT, x:235+c, y:176+d, width:16, height:55, restitution:0.3,density:10 } );
			/*rightLeg_S,26*/ hako[he++] = Shape.create( {shape:Shape.RECT, x:264+c, y:176+d, width:16, height:55, restitution:0.3,density:10 } ); 
			
			/*anchor*/ anc[1] = Shape.create({shape:Shape.CIRCLE,x:258+c, y:d-e,radius:1}); 
			 
			//人間Bのイメージを作る
			cirImg(17,i++);
			recImg(53,28,i++);
			recImg(45,28,i++);
			recImg(40,28,i++);
			recImg(45,25,i++);
			recImg(50,15,i++);
			recImg(50,15,i++);
			recImg(50,15,i++);
			recImg(50,15,i++);
			recImg(16,55,i++);
			recImg(16,55,i++);
			recImg(16,55,i++);
			recImg(16,55,i++);
			
			//人間のオブジェクトをジョイントする
			const b:Number = 13;
			jtR(hako[0+b],hako[1+b],hako[0+b].GetWorldCenter().x,hako[0+b].GetWorldCenter().y,1,-1);
			jtR(hako[1+b],hako[2+b],hako[1+b].GetWorldCenter().x,hako[1+b].GetWorldCenter().y,1,-1);
			jtR(hako[2+b],hako[3+b],hako[2+b].GetWorldCenter().x,hako[2+b].GetWorldCenter().y,1,-1);
			jtR(hako[3+b],hako[4+b],hako[3+b].GetWorldCenter().x,hako[3+b].GetWorldCenter().y,1,-1);
			jtR(hako[1+b],hako[5+b],hako[1+b].GetWorldCenter().x-3,hako[1+b].GetWorldCenter().y,90,-80);
			jtR(hako[6+b],hako[5+b],hako[6+b].GetWorldCenter().x+2,hako[6+b].GetWorldCenter().y,45,-45);
			jtR(hako[1+b],hako[7+b],hako[1+b].GetWorldCenter().x+3,hako[1+b].GetWorldCenter().y,90,-80);
			jtR(hako[8+b],hako[7+b],hako[8+b].GetWorldCenter().x-2,hako[8+b].GetWorldCenter().y,45,-45);
			jtR(hako[4+b],hako[9+b],hako[9+b].GetWorldCenter().x,hako[9+b].GetWorldCenter().y-3,45,-45);
			jtR(hako[9+b],hako[11+b],hako[11+b].GetWorldCenter().x,hako[11+b].GetWorldCenter().y-2,30,-30);
			jtR(hako[4+b],hako[10+b],hako[10+b].GetWorldCenter().x,hako[10+b].GetWorldCenter().y-3,45,-45);
			jtR(hako[10+b],hako[12+b],hako[12+b].GetWorldCenter().x,hako[12+b].GetWorldCenter().y-2,30,-30);
			
			jtR(hako[0+b],anc[1],anc[1].GetWorldCenter().x,anc[1].GetWorldCenter().y,-180,180);
			       		
        		//***************************************************
        		
        		addEventListener(Event.ENTER_FRAME, onEnterFrame);
        		stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
       
        }
		
		private function onEnterFrame(event:Event):void{
			for(var body:b2Body = world.m_bodyList; body; body = body.m_next){
				if(body.m_userData){
					body.m_userData.x = body.GetPosition().x * scale;
					body.m_userData.y = body.GetPosition().y * scale;
					body.m_userData.rotation = body.GetAngle() / (Math.PI / 180);
				} 
			var e:Number = 50;
			if(anc[0].GetWorldCenter().x != hako[0].GetWorldCenter().x) Shape.setVec2(anc[0],hako[0].GetPosition().x*scale,anc[0].GetPosition().y*scale);//アンカー0を人間Aに合わせて動かす
			//if(anc[0].GetWorldCenter().y != hako[0].GetWorldCenter().y) Shape.setVec2(anc[0],anc[0].GetPosition().x,hako[0].GetPosition().y);
			if(anc[1].GetWorldCenter().x != hako[13].GetWorldCenter().x) Shape.setVec2(anc[1],hako[13].GetPosition().x*scale,anc[1].GetPosition().y*scale);//アンカー1を人間Bに合わせて動かす			
			
			
			
			world.Step(0.13 /stage.frameRate, 10);//画面が動くため
		
			}
			if(anc[0].GetWorldCenter().x < 0) pText(0);//勝敗判定
			if(anc[1].GetWorldCenter().x > 62) pText(1);//勝敗判定
			var rnd:Number = Math.floor(Math.random()*15)+1;//乱数の数で敵の強さが変わる
			teki(rnd);
			
		}
		
		private function teki(n:Number):void{
			var i:int = 0;
			if(n<=4){
				for(i=13;i<27;i++)Shape.addVec2(hako[i],-10,0);
				Shape.addVec2(anc[1],-10,0);
			} 
			
			if(n==5 ) Shape.addVec2(hako[19], -150,50);
			if(n==6 ) Shape.addVec2(hako[24], -100,50);
			 
		} 
		
		private function pText(flg:Number):void{
			var tf:TextField = new TextField();
			tf.defaultTextFormat = new TextFormat("_typeWriter", 60, 0x0, true);
			
			if(flg==0) tf.text="You are loser";
			if(flg==1) tf.text="You are winner!!";
			
			tf.border = true;
			tf.background = true;
			tf.x = 50;
			tf.y = stage.stageHeight/2-100;
			tf.autoSize = TextFieldAutoSize.LEFT;
			addChild(tf);
			
		}
		
		private function onKeyDown(e:KeyboardEvent):void{ //キー簿度で操作する
			//var body:b2Body = hako[0];
			var i:int = 0;
			
			if(e.keyCode==38){ //↑を押したら発動 
			//if(e.keyCode==83){//S=83
				//for(i=0;i<3;i++)	Shape.addVec2(hako[i+1],0,-50);
				
			} 
			if(e.keyCode==37){ //←を押したら発動37 
			//if(e.keyCode==64){ //A=64
				for(i=0;i<13;i++)Shape.addVec2(hako[i],-10,0);
					Shape.addVec2(anc[0],-10,0);
			}
			if(e.keyCode==39){ //→押したら発動39 
			//if(e.keyCode==88){ //X=88
				for(i=0;i<13;i++)Shape.addVec2(hako[i],10,0);
				Shape.addVec2(anc[0],10,0);
			}
			 
			if(e.keyCode==70){ //Fキーを押したら発動70  
			//if(e.keyCode==86){//V=86
				Shape.addVec2(hako[8], 80,0);
			}
			
			if(e.keyCode==68){ //Dキーを押したら発動  
			//if(e.keyCode==70){//F=70
				Shape.addVec2(hako[6], 150,-50);
			}
			
			if(e.keyCode==86){ //vキーを押したら発動 
			//if(e.keyCode==67){//c=68
				Shape.addVec2(hako[12], 50,-50);
			}
			
			if(e.keyCode==67){ //Cキーを押したら発動  
			//if(e.keyCode==68){//D=67
				Shape.addVec2(hako[11], 100,-50);
			}
			
			///*敵の操作
			if(e.keyCode==186){ //:を押したら発動
				for(i=13;i<27;i++)	Shape.addVec2(hako[i+1],0,-50);
				
			}
			if(e.keyCode==191){ ///を押したら発動
				for(i=13;i<27;i++)Shape.addVec2(hako[i],-10,0);
					Shape.addVec2(anc[1],-10,0);
			}
			if(e.keyCode==221){ //]押したら発動
				for(i=13;i<27;i++)Shape.addVec2(hako[i],10,0);
				Shape.addVec2(anc[1],10,0);
			}
			
			if(e.keyCode==188){ //,キーを押したら発動
				Shape.addVec2(hako[19], -150,50);
			} 
			
			if(e.keyCode==76){ //Lキーを押したら発動
				Shape.addVec2(hako[20], -150,50);
			}
			
			if(e.keyCode==187){ //;キーを押したら発動
				Shape.addVec2(hako[25], -50,80);
			}
			
			if(e.keyCode==190){ //.キーを押したら発動
				Shape.addVec2(hako[24], -100,50);
			}
			
			
			
			//*/
		}
		
		private function cirImg(n:Number,he:Number):void{	//円のオブジェクトにイメージを貼り付ける	
			var image:Sprite = new Sprite();
			var body:b2Body = hako[he];
			image.graphics.lineStyle(2.0,0x0);
			image.graphics.beginFill(0x0ED6FF);
			image.graphics.drawCircle(0, 0, n);
			image.graphics.endFill();
			
			body.m_userData = addChild(image) as Sprite;
			stage.addChild(image);
			
		}
		
		private function recImg(n:Number,m:Number,he:Number):void{	//四角のオブジェクトにイメージを貼り付ける
			var image:Sprite = new Sprite();
			var body:b2Body = hako[he];
			
			image.graphics.lineStyle(2.0,0x0);
			image.graphics.beginFill(0x0ED6FF);
			image.graphics.drawRect(-n/2,-m/2,n,m);
			image.graphics.endFill();
			
			body.m_userData = addChild(image) as Sprite;
			stage.addChild(image);
			
		}
		
		
		
		public function configureListeners(dispatcher:IEventDispatcher):void{//画像のサイズを変更する
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
		}
		
		public function completeHandler(evt:Event):void{//画像のサイズを変更する
			myLoader.width = stage.stageWidth;
			myLoader.height = stage.stageHeight;
			addChild(myLoader);
		}
		
		public function jtR(bodyA:b2Body, bodyB:b2Body, x:Number, y:Number, uA:Number, lA:Number):void{//Jointを作る
			var jointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
			var jp:b2Vec2 = new b2Vec2(x,y);
			
			jointDef.Initialize(bodyA, bodyB, jp);
			jointDef.lowerAngle = lA / (180/Math.PI);
			jointDef.upperAngle = uA / (180/Math.PI);
			jointDef.enableLimit = false;

			world.CreateJoint(jointDef);
		}
		
    }
}

import flash.display.Sprite;
import Box2D.Collision.Shapes.b2CircleDef;
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.Shapes.b2ShapeDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;

class Shape{
	public static const CIRCLE:int = 0;
	public static const RECT:int = 1;
	
	public static var world:b2World;
	public static var scale:Number;
	
	public static function addVec2(body:b2Body, x:Number, y:Number):void{ //オブジェクトの位置を、既存の値にプラスして移動する
		body.SetXForm(new b2Vec2(body.GetPosition().x + x /scale, body.GetPosition().y + y / scale), body.GetAngle());
	}
	
	public static function setVec2(body:b2Body, x:Number, y:Number):void{ //オブジェクトの位置をセットする
		body.SetXForm(new b2Vec2(x / scale, y / scale), body.GetAngle());
	}
	
	public static function addAngle(body:b2Body,rotate:Number):void{ //オブジェクトの角度を、既存の値にプラスして移動する
		body.SetXForm(body.GetPosition(), body.GetAngle() + rotate * Math.PI / 180);
	}
	
	public static function setAngle(body:b2Body, rotate:Number):void{ //オブジェクトの角度をセットする
		body.SetXForm(body.GetPosition(), rotate * Math.PI / 180);
	}
	
	public static function create(params:Object):b2Body{  //オブジェクトを作成する
		var def:b2BodyDef = new b2BodyDef();
		var image:Sprite = new Sprite();
		
		if (params.angle) def.angle = params.angle * Math.PI / 180; //
		
		var shape:b2ShapeDef;
		if (params.shape == Shape.RECT){ //四角オブジェクトを作成
			def.position.Set((params.x + params.width /2) / scale, (params.y + params.height / 2) / scale);
			shape = new b2PolygonDef();
			b2PolygonDef(shape).SetAsBox(params.width / 2/ scale, params.height / 2 / scale);
			 
			
		}
		else if (params.shape == Shape.CIRCLE){ //円オブジェクトを作成
			def.position.Set(params.x / scale, params.y / scale);
			shape = new b2CircleDef();
			b2CircleDef(shape).radius = params.radius / scale;
			
		}
	
		if (params.cBits){
			shape.filter.categoryBits = params.cBits;
			if (params.mBits) shape.filter.maskBits = params.mBits;
			else shape.filter.maskBits = params.cBits;
		}

	
		shape.density = params.density; 	//オブジェクトに密度を設定
		shape.restitution = params.restitution;  //オブジェクトに反発係数を設定
		//shape.friction = params.friction;  //オブジェクトに摩擦係数を設定
		
		var body:b2Body = world.CreateBody(def);
		body.CreateShape(shape);
		
		if (shape.density > 0) body.SetMassFromShapes(); //オブジェクトに密度名がない場合、移動をしない
		
		return body;
	}
}