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

プロとして恥ずかしくないActionScript3.0さんぶる

Get Adobe Flash player
by albatrus_jp 13 Feb 2010
    Embed
/**
 * Copyright albatrus_jp ( http://wonderfl.net/user/albatrus_jp )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dZ9j
 */

package {
	
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.utils.Timer;
	import flash.events.Event;
	import flash.events.TimerEvent;
	
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	
	
	
	public class Physics extends Sprite {
		
		private var m_world:b2World;
		private var m_scale:Number = 30;	
		
		
		
		public function Physics(){
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-100.0, -100.0);
			worldAABB.upperBound.Set(100.0, 100.0);
			
			var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
			
			var doSleep:Boolean = true;
			
			m_world = new b2World(worldAABB, gravity, doSleep);
			
			var body:b2Body;
			var bodyDef:b2BodyDef;
			var boxDef:b2PolygonDef;
			var circleDef:b2CircleDef;
			
			var insWidth:Number = 0;
			var insHeight:Number = stage.stageHeight / m_scale;
			
			var floorWidth:Number = stage.stageWidth / m_scale;
			var floorHeight:Number = 3;
			
			bodyDef = new b2BodyDef();
			bodyDef.position.Set(insWidth, insHeight + floorHeight);
			
			boxDef = new b2PolygonDef();
			boxDef.SetAsBox(floorWidth, floorHeight);
			boxDef.friction = 0.3;
			boxDef.density = 0;
			
			body = m_world.CreateBody(bodyDef);
			//落下箱物体に形状を与えます
			body.CreateShape(boxDef);
			//塊属性を現在の形状から自動生成します
			body.SetMassFromShapes();
			
			
			
			var timer:Timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
			timer.start();
			
			
			
			addEventListener(Event.ENTER_FRAME, onUpdateHandler);
		}
		
		
		
		private function onUpdateHandler(event:Event = null):void {
			var m_iterations:int = 20;
			var m_timeStep:Number = 1.0 / 30.0;
			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 * 30;
					bb.m_userData.y = bb.GetPosition().y * 30;
					bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
				}
			}
		}
		
		
		
		private function onTimerHandler(event:Event):void {
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set(Math.random() * stage.stageWidth / m_scale, -1);
			var rX:Number =Math.random() + 0.8;
			var rY:Number = Math.random() + 0.8;
			
			var body:b2Body;
			var userData:Sprite = new Sprite();
			var graphics:Graphics = userData.graphics;
			graphics.beginFill(Math.random()*0xffffff, 1);
		
			
			if (Math.random() < 0.33){
				var boxDef:b2PolygonDef = new b2PolygonDef();
				boxDef.SetAsBox(rX, rY);
				boxDef.density = 1.0;
				boxDef.friction = 0.5;
				boxDef.restitution = 0.2;
				
				graphics.drawRect( -rX / 2, -rY / 2, rX, rY);
				graphics.endFill();
				
				bodyDef.userData = userData;
				bodyDef.userData.width = rX * 2 * 30;
				bodyDef.userData.height = rY * 2 * 30;
				body = m_world.CreateBody(bodyDef);
				body.CreateShape(boxDef);
				
			} else if(Math.random() < 0.66) {
				var circleDef:b2CircleDef = new b2CircleDef();
				circleDef.radius = rX;
				circleDef.density = 1.0;
				circleDef.friction = 0.5;
				circleDef.restitution = 0.3;
				
				graphics.drawCircle( 0, 0, rX);
				graphics.endFill();
				
				bodyDef.userData = userData;
				bodyDef.userData.width = rX * 2 * 30;
				bodyDef.userData.height = rX * 2 * 30;
				body = m_world.CreateBody(bodyDef);
				body.CreateShape(circleDef);
				
			} else {
				var radius:Number = rX;
				var triangle_h:Number = Math.tan(60 * Math.PI / 180) * radius;
				
				var tryangleDef:b2PolygonDef = new b2PolygonDef();
				tryangleDef.vertexCount = 3;
				tryangleDef.vertices[0].Set( -1 * radius, -1 * triangle_h / 2);
				tryangleDef.vertices[1].Set( radius, -1 * triangle_h / 2);
				tryangleDef.vertices[2].Set( 0, triangle_h / 2);
				tryangleDef.density = 1.0;
				tryangleDef.friction = 0.5;
				tryangleDef.restitution = 0.2;
				
				graphics.moveTo( -1 * radius, -1 * triangle_h / 2);
				graphics.lineTo( radius, -1 * triangle_h / 2);
				graphics.lineTo( 0, triangle_h / 2);
				graphics.endFill();
				
				bodyDef.userData = userData;
				bodyDef.userData.width = rX * 2 * 30;
				bodyDef.userData.height = rX * 2 * 30;
				body = m_world.CreateBody(bodyDef);
				body.CreateShape(tryangleDef);
			}
			
			body.SetMassFromShapes();
			addChild(bodyDef.userData);
			
			onUpdateHandler();
		}
	}
}