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

forked from: box2dでボールを落とす.as

クリックすると赤いボール落ちます。
// forked from izm_design's box2dでボールを落とす.as
package 
{

        //クリックすると赤いボール落ちます。
    
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Dynamics.Joints.*;
	import Box2D.Dynamics.Contacts.*;
	import Box2D.Common.*;
	import Box2D.Common.Math.*;

	[SWF(width="465", height="465",backgroundColor = "0xFFFFFF", frameRate = "45")]
	public class main extends Sprite
	{
		
		public var m_sprite:Sprite;
		public var m_world:b2World;
		public var m_physScale:Number = 30.0;
		public var m_iterations:int = 10;
		public var m_timeStep:Number = 1.0 / 30.0;
		
		//動的オブジェクト
		private var bodyDef:b2BodyDef;
		private var body:b2Body;
		private var boxDef:b2CircleDef;
		private var bodyArr:Array = [];
		
		
		public var urlNum:int = 0;
		public var loadTarget:String = "img_s/";
		
		public function main ()
		{
			m_sprite = new Sprite();
			addChild (m_sprite);
			
			addEventListener (Event.ENTER_FRAME, update, false, 0, true);
			
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEvent);
			
			////////////////////////////////////////// WORLD
			
			//WORLD作成
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set (-1000.0, -1000.0);
			worldAABB.upperBound.Set (1000.0, 1000.0);
			//重力
			var gravity:b2Vec2 = new b2Vec2(0.0, 20.0);
			// Bodyオブジェクトのスリープ
			var doSleep:Boolean = true;
			//シミュレーション空間
			m_world = new b2World(worldAABB, gravity, doSleep);
			
			////////////////////////////////////////// デバック用
			/*
			var dbgDraw:b2DebugDraw= new b2DebugDraw();
			var dbgSprite:Sprite= new Sprite();
			m_sprite.addChild (dbgSprite);
			dbgDraw.m_sprite= dbgSprite;
			dbgDraw.m_drawScale= 30.0;
			dbgDraw.m_fillAlpha= 0.3;
			dbgDraw.m_lineThickness= 1.0;
			dbgDraw.m_alpha=1.0;
			dbgDraw.m_xformScale=1.0;
			dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit;
			m_world.SetDebugDraw (dbgDraw);
			*/
			////////////////////////////////////////// 静止オブジェクト
			
			var wallObject:b2PolygonDef = new b2PolygonDef();
			wallObject.density = 0.0;
			var wallDef:b2BodyDef = new b2BodyDef();
			var wallBody:b2Body;
			////////////////// Left
			wallDef.position.Set (10/2 / m_physScale, 465/2 / m_physScale);
			wallObject.SetAsBox (10/2 / m_physScale, 465/2 / m_physScale);
			wallBody = m_world.CreateBody(wallDef);
			wallBody.CreateShape (wallObject);
			wallBody.SetMassFromShapes ();
			
			////////////////// Right
			wallDef.position.Set ((465-10/2) / m_physScale, 465/2 / m_physScale);
			wallObject.SetAsBox (10/2 / m_physScale, 600/2 / m_physScale);
			wallBody = m_world.CreateBody(wallDef);
			wallBody.CreateShape (wallObject);
			wallBody.SetMassFromShapes ();
			
			////////////////// Top
			wallDef.position.Set (465/2 / m_physScale, 10/2 / m_physScale);
			wallObject.SetAsBox (465/2 / m_physScale, 10/2 / m_physScale);
			wallBody = m_world.CreateBody(wallDef);
			wallBody.CreateShape (wallObject);
			wallBody.SetMassFromShapes ();
			
			////////////////// Bottom
			wallDef.position.Set (465/2 / m_physScale, 465 / m_physScale);
			wallObject.SetAsBox (465/2 / m_physScale, 10/2 / m_physScale);
			wallBody = m_world.CreateBody(wallDef);
			wallBody.CreateShape (wallObject);
			wallBody.SetMassFromShapes ();
			
			addObj();
			
		}
		////////////////////////////////////////// オブジェクト生成
		
		private function mouseUpEvent(e:MouseEvent) :void{
			addObj();	
		}
		
		private function addObj ():void
		{	
			bodyDef = new b2BodyDef();
			bodyDef.position.Set ( mouseX / m_physScale, mouseY/ m_physScale);
			
			boxDef = new b2CircleDef();
			boxDef.density      = 1.5;//密度
			boxDef.friction     = 0.5;
			boxDef.restitution  = 1.0;//反発係数
			
			
			//ここを変更する
			var mc:Sprite=new Sprite();
			mc.graphics.beginFill (0xFF0000);
			mc.graphics.drawCircle(0, 0, 50);;
			mc.graphics.endFill ();
				
			bodyDef.userData = mc;
			bodyDef.userData.x = -1000;
			bodyDef.userData.y = -1000;
			bodyDef.userData.width = 50;
			bodyDef.userData.height = 50;
			
			//boxDef.(mc.width/2 / m_physScale, mc.height/2 / m_physScale);
			
			body = m_world.CreateBody(bodyDef);
			body.CreateShape (boxDef);
			body.SetMassFromShapes ();
			addChild (bodyDef.userData);
			
			bodyArr.push (body);
			
			if (bodyArr.length > 15) {
				destroyDynamicObject ();
			}
		}
		////////////////////////////////////////////////// オブジェクト消す
		private function destroyDynamicObject ():void
		{
			var tempArr:Array = bodyArr.splice(0, 1);
			for (var i:int = 0; i < tempArr.length; i++) {
				var body:b2Body = tempArr[i];
				var bodyData:Sprite = Sprite(body.GetUserData());
				removeChild (bodyData);
				m_world.DestroyBody (body);
			}
		}
		////////////////////////////////////////// 描画
		private function update (event:Event):void
		{
			m_world.Step (m_timeStep, m_iterations);
			for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
				if (bb.m_userData is Sprite) {
					bb.m_userData.x = bb.GetPosition().x * m_physScale;
					bb.m_userData.y = bb.GetPosition().y * m_physScale;
				}
			}
		}
	}
}