Return to Snippet

Revision: 20525
at November 17, 2009 19:26 by jamesbull


Initial Code
package {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
 
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
 
	public class TextFalls extends Sprite {
		public var the_world:b2World;
		public var physScale:Number=30;
		public var final_body:b2Body;
		public var the_body:b2BodyDef;
		public var the_box:b2PolygonDef;
		public var the_circle:b2CircleDef;
		public var environment:b2AABB = new b2AABB();
		public var gravity:b2Vec2=new b2Vec2(0.0,1);
		
		public var dropTextTime:Timer;
		public var linkbtn:Timer;
		public var blockTrackingArray:Array = new Array();
		public var labelTxt:TextField = new TextField();
		public var labelTrackingArray:Array = new Array();
		public var wordsArray:Array = new Array();
		public var slot=0;
		public var Text:String="SPACES CONNECT PEOPLE "
		
		public var spriteArray:Array = new Array();
		public var holderSprite:Sprite = new Sprite();
 
		public function TextFalls() {
 
			environment.lowerBound.Set(-100.0, -100.0);
			environment.upperBound.Set(100.0, 100.0);
			the_world=new b2World(environment,gravity,true);
 
			buildWalls();
 
			addEventListener(Event.ENTER_FRAME, on_enter_frame);
			
			dropTextTime=new Timer(2000, 30);
			dropTextTime.addEventListener(TimerEvent.TIMER, newText);
			dropTextTime.start();
			
			linkbtn=new Timer(2000, 1);
			linkbtn.addEventListener(TimerEvent.TIMER, newbtn);
			linkbtn.start();
			
			wordsArray=Text.split(" ");
		}
 
		public function buildWalls() {
			//This Turns on The Ability to View Where the Physics Blockades are (best to get used to the spacing)
			/*var debug_draw:b2DebugDraw = new b2DebugDraw();
			var debug_sprite:Sprite = new Sprite();
			addChild(debug_sprite);
			debug_draw.m_sprite=debug_sprite;
			debug_draw.m_drawScale=30;
			debug_draw.m_fillAlpha=0.5;
			debug_draw.m_lineThickness=1;
			debug_draw.m_drawFlags=b2DebugDraw.e_shapeBit;
			the_world.SetDebugDraw(debug_draw);*/
 
			the_body = new b2BodyDef();
			the_body.position.Set(1238 / 2 / physScale, 682 / physScale);//physScale is used to make calculating time based physics and not frame based.
			the_box = new b2PolygonDef();
			the_box.SetAsBox( 619 / physScale, 1 / physScale);
			the_box.friction=0.3;
			the_box.density=0;
			final_body=the_world.CreateBody(the_body);
			final_body.CreateShape(the_box);
			final_body.SetMassFromShapes();
 
			the_body = new b2BodyDef();
			the_body.position.Set(200 / physScale, 300 / physScale);
			the_circle = new b2CircleDef();
			the_circle.radius=0.4;
			the_circle.density=0;
			final_body=the_world.CreateBody(the_body);
			final_body.CreateShape(the_circle);
			final_body.SetMassFromShapes();
			
			/*the_body = new b2BodyDef();
			the_body.position.Set(600 / physScale, 200 / physScale);
			the_circle = new b2CircleDef();
			the_circle.radius=1.3;
			the_circle.density=0;
			final_body=the_world.CreateBody(the_body);
			final_body.CreateShape(the_circle);
			final_body.SetMassFromShapes();*/
			
			the_body = new b2BodyDef();
			the_body.position.Set(1000 / physScale, 200 / physScale);
			the_circle = new b2CircleDef();
			the_circle.radius=0.4;
			the_circle.density=0;
			final_body=the_world.CreateBody(the_body);
			final_body.CreateShape(the_circle);
			final_body.SetMassFromShapes();
 
		}
		
		private function NewTextLabel():void {
			labelTxt = new TextField();
			labelTxt.autoSize=TextFieldAutoSize.LEFT;
			labelTxt.cacheAsBitmap=true;
			labelTxt.embedFonts=true;
			labelTxt.x=-100;
			
			var format:TextFormat = new TextFormat();
			format.font="Aller Display";
			format.color=0xcccccc;
			if (wordsArray[slot].length==0){
				++slot;
			}
			if (slot>=wordsArray.length-1){
				slot=0;
			}
			labelTxt.text=wordsArray[slot];
			format.size=55;
			labelTxt.defaultTextFormat=format;
			labelTxt.text=wordsArray[slot];
			++slot;
		}
		
		public function newText(evt:TimerEvent):void{
			NewTextLabel();
			holderSprite = new Sprite();
			addChild(holderSprite);
			holderSprite.addChild(labelTxt);
			labelTxt.x=holderSprite.x-labelTxt.width/2;
			labelTxt.y=holderSprite.y-labelTxt.height/2;
			holderSprite.x = -1000;
			holderSprite.y = -1000;
 
			spriteArray.push(holderSprite);
			labelTrackingArray.push(labelTxt);
			
			var final_body:b2Body;
			var the_body:b2BodyDef;
			var the_box:b2PolygonDef;
			the_body = new b2BodyDef();
			the_body.position.Set(Math.random()*35+2, -1);
			the_box = new b2PolygonDef();
			the_box.SetAsBox(labelTxt.width/physScale/2, labelTxt.height/physScale/3);
			the_box.friction=.7;
			the_box.density=1;
			the_box.restitution=0.2;
			final_body=the_world.CreateBody(the_body);
			final_body.CreateShape(the_box);
			final_body.SetMassFromShapes();
			blockTrackingArray.push(final_body);
			
		}
 
		public function on_enter_frame(evt:Event) {
			the_world.Step(1/30, 10);
 
			var detroyAt:Array = new Array();
			var totalBlocks=blockTrackingArray.length;
 
			for (var b = 0; b < totalBlocks; ++b) {
				if (blockTrackingArray[b].GetPosition().y>768/physScale) {//Kills Block if it fall past the screen.
					spriteArray[b].removeChild(labelTrackingArray[b]);
					removeChild(spriteArray[b]);
					the_world.DestroyBody(blockTrackingArray[b]);
					detroyAt.push(b);
				}
				var bodyRotation:Number=blockTrackingArray[b].GetAngle();
				spriteArray[b].rotation = bodyRotation  * (180/Math.PI) % 360;
				spriteArray[b].x = (blockTrackingArray[b].GetPosition().x * physScale);
				spriteArray[b].y = (blockTrackingArray[b].GetPosition().y * physScale);
			}
 
			for (var d = detroyAt.length-1; d >= 0; --d) {
				blockTrackingArray.splice(detroyAt[d], 1);
				labelTrackingArray.splice(detroyAt[d], 1);
				spriteArray.splice(detroyAt[d], 1);
				detroyAt.pop();
			}
		}
		
		public function newbtn(myEvent:TimerEvent):void{
			 labelTxt.addEventListener(MouseEvent.MOUSE_OVER, btnHover);
				function btnHover(myEvent, MouseEvent):void{
					var format:TextFormat = new TextFormat();
					format.color=0xcccccc;
				}
		}
	}
}

Initial URL
http://jamesbull.ca/projects/wax/orda/HelloWorld.html

Initial Description


Initial Title
Box2DFlash Falling Text

Initial Tags


Initial Language
ActionScript 3