PageRenderTime 87ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ micro-physics-particle --username jack.eubert@gmail.com/demo/CrazeArrow.as

http://micro-physics-particle.googlecode.com/
ActionScript | 218 lines | 193 code | 22 blank | 3 comment | 19 complexity | 9b0c2e5d6f4daa667fb692e75846c80c MD5 | raw file
  1. package
  2. {
  3. import cn.eubert.mpp.canvas.CanvasBMD;
  4. import cn.eubert.mpp.particles.*;
  5. import cn.eubert.mpp.physics.forces.Attraction;
  6. import cn.eubert.mpp.physics.forces.Brownian;
  7. import cn.eubert.mpp.physics.forces.Repulsion;
  8. import cn.eubert.mpp.util.FPS;
  9. import flash.display.Bitmap;
  10. import flash.display.BlendMode;
  11. import flash.display.Sprite;
  12. import flash.display.StageAlign;
  13. import flash.display.StageQuality;
  14. import flash.display.StageScaleMode;
  15. import flash.events.Event;
  16. import flash.events.KeyboardEvent;
  17. import flash.events.MouseEvent;
  18. import flash.geom.Matrix;
  19. import flash.geom.Point;
  20. import flash.geom.Rectangle;
  21. import flash.text.TextField;
  22. /**
  23. * @author eubert
  24. */
  25. [SWF(backgroundColor="#000000", frameRate="30", width="1000", height="800")]
  26. public class CrazeArrow extends Sprite
  27. {
  28. public var sttractionPoint : Point;
  29. private var ps : ParticlesSystem;
  30. private var bound : Rectangle;
  31. private var canvase : Bitmap;
  32. private var bmd : CanvasBMD;
  33. private var scale : Number = 0.6;
  34. [Embed(source="assets/arrow.png")]
  35. public var arrowPic : Class;
  36. public var arrowBmd : CanvasBMD;
  37. public var arrowBmp : Bitmap;
  38. public var bmpContainer : Sprite = new Sprite();
  39. public var mat : Matrix = new Matrix();
  40. public var bmdArray : Array = [];
  41. private var r : Number = 255;
  42. private var g : Number = 127;
  43. private var b : Number = 0;
  44. private var ri : Number = 0.02;
  45. private var gi : Number = 0.015;
  46. private var bi : Number = 0.025;
  47. private var pause:Boolean;
  48. public function CrazeArrow()
  49. {
  50. this.addEventListener(Event.ADDED_TO_STAGE, init);
  51. }
  52. private function init(e : Event) : void
  53. {
  54. stageSetup();
  55. ps = new ParticlesSystem(BoundParticle);
  56. sttractionPoint = new Point();
  57. initBmdArray();
  58. boom(2500);
  59. resizeHandler(null);
  60. ps.startRendering();
  61. this.addEventListener(Event.ENTER_FRAME, loop);
  62. addChild(new FPS());
  63. initTitleText();
  64. }
  65. private function initTitleText():void
  66. {
  67. var titleTxt:TextField=new TextField();
  68. titleTxt.text="Key 'p:'pause, Move your mouse and click the stage.";
  69. titleTxt.textColor=0xe1e1e1;
  70. titleTxt.autoSize="left";
  71. titleTxt.selectable=false;
  72. titleTxt.x=5;
  73. titleTxt.y=20;
  74. addChild(titleTxt);
  75. }
  76. private function stageSetup() : void
  77. {
  78. stage.align = StageAlign.TOP_LEFT;
  79. stage.scaleMode = StageScaleMode.NO_SCALE;
  80. stage.quality = StageQuality.LOW;
  81. stage.doubleClickEnabled = true;
  82. stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
  83. stage.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
  84. stage.addEventListener(MouseEvent.DOUBLE_CLICK, mouseHandler);
  85. stage.addEventListener(Event.RESIZE, resizeHandler);
  86. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
  87. }
  88. private function keyDownHandler(event : KeyboardEvent) : void
  89. {
  90. if (event.keyCode == 80)
  91. {
  92. if(pause)
  93. {
  94. this.addEventListener(Event.ENTER_FRAME, loop);
  95. ps.startRendering();
  96. pause=false;
  97. }else
  98. {
  99. this.removeEventListener(Event.ENTER_FRAME, loop);
  100. ps.stopRendering();
  101. pause=true;
  102. }
  103. }
  104. }
  105. private function initBmdArray() : void
  106. {
  107. arrowBmp = new arrowPic();
  108. arrowBmd = new CanvasBMD(arrowBmp.width, arrowBmp.height, true, 0);
  109. for(var i : int = 0;i < 361;i++)
  110. {
  111. mat = new Matrix();
  112. mat.translate(-arrowBmp.width / 2, -arrowBmp.height / 2);
  113. mat.rotate(i * (Math.PI / 180));
  114. mat.translate(arrowBmp.width / 2, arrowBmp.height / 2);
  115. var bmd : CanvasBMD = new CanvasBMD(arrowBmp.width, arrowBmp.height, true, 0);
  116. bmd.draw(arrowBmp.bitmapData, mat);
  117. bmdArray.push(bmd);
  118. }
  119. }
  120. private function resizeHandler(e : Event) : void
  121. {
  122. if (bmd != null) bmd.dispose();
  123. if (canvase != null) removeChild(canvase);
  124. bmd = new CanvasBMD(stage.stageWidth * scale, stage.stageHeight * scale, false, 0);
  125. canvase = new Bitmap(bmd);
  126. canvase.blendMode = BlendMode.ADD;
  127. canvase.width = stage.stageWidth;
  128. canvase.height = stage.stageHeight;
  129. addChild(canvase);
  130. var i : int;
  131. i = ps.particles.length;
  132. bound = new Rectangle(0, 0, stage.stageWidth * scale, stage.stageHeight * scale);
  133. while (i-- > 0)
  134. {
  135. ps.particles[i].particleBound = bound;
  136. }
  137. }
  138. private function mouseHandler(event : MouseEvent) : void
  139. {
  140. var i : int;
  141. if (event.type == MouseEvent.MOUSE_DOWN)
  142. {
  143. i = ps.particles.length;
  144. while (i-- > 0)
  145. {
  146. ps.particles[i].removeForce("repulsionForce");
  147. var attractionForce : Attraction = new Attraction(sttractionPoint, 1.5, 80);
  148. ps.particles[i].addForce("attractionForce", attractionForce);
  149. }
  150. }
  151. else if (event.type == MouseEvent.MOUSE_UP)
  152. {
  153. i = ps.particles.length;
  154. while (i-- > 0)
  155. {
  156. ps.particles[i].removeForce("attractionForce");
  157. var repulsionForce : Repulsion = new Repulsion(sttractionPoint, 8, 100);
  158. ps.particles[i].addForce("repulsionForce", repulsionForce);
  159. }
  160. }
  161. }
  162. private function loop(e : Event) : void
  163. {
  164. var i : int = ps.particles.length;
  165. bmd.lock();
  166. while (i-- > 0)
  167. {
  168. var angle : Number = ps.particles[i].dir;
  169. if(angle < 0)
  170. {
  171. angle = 360 + angle;
  172. }
  173. bmd.copyPixels(bmdArray[int(angle % 360)], arrowBmd.rect, new Point(ps.particles[i].x, ps.particles[i].y));
  174. }
  175. bmd.colorOffset(-(Math.sin(r += ri) * 128 + 127)-15, -(Math.sin(g += gi) * 128 + 127)-15 ,-(Math.sin(b += bi) * 128 + 127)-15, 0);
  176. sttractionPoint.x = mouseX * scale;
  177. sttractionPoint.y = mouseY * scale;
  178. bmd.unlock();
  179. }
  180. public function boom(count : int = 30) : void
  181. {
  182. bound = new Rectangle(0, 0, stage.stageWidth * scale, stage.stageHeight * scale);
  183. for (var i : int = 0;i < count;i++)
  184. {
  185. var fireParticle : BoundParticle = BoundParticle(ps.createParticle());
  186. fireParticle.bounceIntensity = 2;
  187. fireParticle.particleBound = bound;
  188. var repulsionForce : Repulsion = new Repulsion(sttractionPoint, 8, 100);
  189. fireParticle.addForce("repulsionForce", repulsionForce);
  190. var brownForce : Brownian = new Brownian(0.5, 1);
  191. fireParticle.addForce("brown", brownForce);
  192. fireParticle.init(stage.stageWidth *scale/2, stage.stageHeight *scale/2);
  193. }
  194. }
  195. }
  196. }