Posted By

jamesbull on 11/17/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

forze


Box2DFlash Falling Text


 / Published in: ActionScript 3
 

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

  1. package {
  2. import flash.display.Sprite;
  3. import flash.text.TextField;
  4. import flash.text.TextFieldAutoSize;
  5. import flash.text.TextFormat;
  6. import flash.events.Event;
  7. import flash.utils.Timer;
  8. import flash.events.TimerEvent;
  9. import flash.events.MouseEvent;
  10.  
  11. import Box2D.Dynamics.*;
  12. import Box2D.Collision.*;
  13. import Box2D.Collision.Shapes.*;
  14. import Box2D.Common.Math.*;
  15.  
  16. public class TextFalls extends Sprite {
  17. public var the_world:b2World;
  18. public var physScale:Number=30;
  19. public var final_body:b2Body;
  20. public var the_body:b2BodyDef;
  21. public var the_box:b2PolygonDef;
  22. public var the_circle:b2CircleDef;
  23. public var environment:b2AABB = new b2AABB();
  24. public var gravity:b2Vec2=new b2Vec2(0.0,1);
  25.  
  26. public var dropTextTime:Timer;
  27. public var linkbtn:Timer;
  28. public var blockTrackingArray:Array = new Array();
  29. public var labelTxt:TextField = new TextField();
  30. public var labelTrackingArray:Array = new Array();
  31. public var wordsArray:Array = new Array();
  32. public var slot=0;
  33. public var Text:String="SPACES CONNECT PEOPLE "
  34.  
  35. public var spriteArray:Array = new Array();
  36. public var holderSprite:Sprite = new Sprite();
  37.  
  38. public function TextFalls() {
  39.  
  40. environment.lowerBound.Set(-100.0, -100.0);
  41. environment.upperBound.Set(100.0, 100.0);
  42. the_world=new b2World(environment,gravity,true);
  43.  
  44. buildWalls();
  45.  
  46. addEventListener(Event.ENTER_FRAME, on_enter_frame);
  47.  
  48. dropTextTime=new Timer(2000, 30);
  49. dropTextTime.addEventListener(TimerEvent.TIMER, newText);
  50. dropTextTime.start();
  51.  
  52. linkbtn=new Timer(2000, 1);
  53. linkbtn.addEventListener(TimerEvent.TIMER, newbtn);
  54. linkbtn.start();
  55.  
  56. wordsArray=Text.split(" ");
  57. }
  58.  
  59. public function buildWalls() {
  60. //This Turns on The Ability to View Where the Physics Blockades are (best to get used to the spacing)
  61. /*var debug_draw:b2DebugDraw = new b2DebugDraw();
  62. var debug_sprite:Sprite = new Sprite();
  63. addChild(debug_sprite);
  64. debug_draw.m_sprite=debug_sprite;
  65. debug_draw.m_drawScale=30;
  66. debug_draw.m_fillAlpha=0.5;
  67. debug_draw.m_lineThickness=1;
  68. debug_draw.m_drawFlags=b2DebugDraw.e_shapeBit;
  69. the_world.SetDebugDraw(debug_draw);*/
  70.  
  71. the_body = new b2BodyDef();
  72. the_body.position.Set(1238 / 2 / physScale, 682 / physScale);//physScale is used to make calculating time based physics and not frame based.
  73. the_box = new b2PolygonDef();
  74. the_box.SetAsBox( 619 / physScale, 1 / physScale);
  75. the_box.friction=0.3;
  76. the_box.density=0;
  77. final_body=the_world.CreateBody(the_body);
  78. final_body.CreateShape(the_box);
  79. final_body.SetMassFromShapes();
  80.  
  81. the_body = new b2BodyDef();
  82. the_body.position.Set(200 / physScale, 300 / physScale);
  83. the_circle = new b2CircleDef();
  84. the_circle.radius=0.4;
  85. the_circle.density=0;
  86. final_body=the_world.CreateBody(the_body);
  87. final_body.CreateShape(the_circle);
  88. final_body.SetMassFromShapes();
  89.  
  90. /*the_body = new b2BodyDef();
  91. the_body.position.Set(600 / physScale, 200 / physScale);
  92. the_circle = new b2CircleDef();
  93. the_circle.radius=1.3;
  94. the_circle.density=0;
  95. final_body=the_world.CreateBody(the_body);
  96. final_body.CreateShape(the_circle);
  97. final_body.SetMassFromShapes();*/
  98.  
  99. the_body = new b2BodyDef();
  100. the_body.position.Set(1000 / physScale, 200 / physScale);
  101. the_circle = new b2CircleDef();
  102. the_circle.radius=0.4;
  103. the_circle.density=0;
  104. final_body=the_world.CreateBody(the_body);
  105. final_body.CreateShape(the_circle);
  106. final_body.SetMassFromShapes();
  107.  
  108. }
  109.  
  110. private function NewTextLabel():void {
  111. labelTxt = new TextField();
  112. labelTxt.autoSize=TextFieldAutoSize.LEFT;
  113. labelTxt.cacheAsBitmap=true;
  114. labelTxt.embedFonts=true;
  115. labelTxt.x=-100;
  116.  
  117. var format:TextFormat = new TextFormat();
  118. format.font="Aller Display";
  119. format.color=0xcccccc;
  120. if (wordsArray[slot].length==0){
  121. ++slot;
  122. }
  123. if (slot>=wordsArray.length-1){
  124. slot=0;
  125. }
  126. labelTxt.text=wordsArray[slot];
  127. format.size=55;
  128. labelTxt.defaultTextFormat=format;
  129. labelTxt.text=wordsArray[slot];
  130. ++slot;
  131. }
  132.  
  133. public function newText(evt:TimerEvent):void{
  134. NewTextLabel();
  135. holderSprite = new Sprite();
  136. addChild(holderSprite);
  137. holderSprite.addChild(labelTxt);
  138. labelTxt.x=holderSprite.x-labelTxt.width/2;
  139. labelTxt.y=holderSprite.y-labelTxt.height/2;
  140. holderSprite.x = -1000;
  141. holderSprite.y = -1000;
  142.  
  143. spriteArray.push(holderSprite);
  144. labelTrackingArray.push(labelTxt);
  145.  
  146. var final_body:b2Body;
  147. var the_body:b2BodyDef;
  148. var the_box:b2PolygonDef;
  149. the_body = new b2BodyDef();
  150. the_body.position.Set(Math.random()*35+2, -1);
  151. the_box = new b2PolygonDef();
  152. the_box.SetAsBox(labelTxt.width/physScale/2, labelTxt.height/physScale/3);
  153. the_box.friction=.7;
  154. the_box.density=1;
  155. the_box.restitution=0.2;
  156. final_body=the_world.CreateBody(the_body);
  157. final_body.CreateShape(the_box);
  158. final_body.SetMassFromShapes();
  159. blockTrackingArray.push(final_body);
  160.  
  161. }
  162.  
  163. public function on_enter_frame(evt:Event) {
  164. the_world.Step(1/30, 10);
  165.  
  166. var detroyAt:Array = new Array();
  167. var totalBlocks=blockTrackingArray.length;
  168.  
  169. for (var b = 0; b < totalBlocks; ++b) {
  170. if (blockTrackingArray[b].GetPosition().y>768/physScale) {//Kills Block if it fall past the screen.
  171. spriteArray[b].removeChild(labelTrackingArray[b]);
  172. removeChild(spriteArray[b]);
  173. the_world.DestroyBody(blockTrackingArray[b]);
  174. detroyAt.push(b);
  175. }
  176. var bodyRotation:Number=blockTrackingArray[b].GetAngle();
  177. spriteArray[b].rotation = bodyRotation * (180/Math.PI) % 360;
  178. spriteArray[b].x = (blockTrackingArray[b].GetPosition().x * physScale);
  179. spriteArray[b].y = (blockTrackingArray[b].GetPosition().y * physScale);
  180. }
  181.  
  182. for (var d = detroyAt.length-1; d >= 0; --d) {
  183. blockTrackingArray.splice(detroyAt[d], 1);
  184. labelTrackingArray.splice(detroyAt[d], 1);
  185. spriteArray.splice(detroyAt[d], 1);
  186. detroyAt.pop();
  187. }
  188. }
  189.  
  190. public function newbtn(myEvent:TimerEvent):void{
  191. labelTxt.addEventListener(MouseEvent.MOUSE_OVER, btnHover);
  192. function btnHover(myEvent, MouseEvent):void{
  193. var format:TextFormat = new TextFormat();
  194. format.color=0xcccccc;
  195. }
  196. }
  197. }
  198. }

Report this snippet  

You need to login to post a comment.