topical media & game development

talk show tell print

animation-ch19-Brownian1.ax

animation-ch19-Brownian1.ax [swf] [flash] flex


  package {
          import flash.display.Sprite;
          import flash.events.Event;
  
          public class @ax-animation-ch19-Brownian1 extends Sprite
          {
                  private var numDots:uint = 50;
                  private var friction:Number = 0.95;
                  private var dots:Array;
                  
                  public function @ax-animation-ch19-Brownian1()
                  {
                          init();
                  }
                  
                  private function init():void
                  {
                          dots = new Array();
                          for(var i:uint = 0; i < numDots; i++)
                          {
                                  var dot:animation_ch19_Ball = new animation_ch19_Ball(1, 0);
                                  dot.x = Math.random() * stage.stageWidth;
                                  dot.y = Math.random() * stage.stageHeight;
                                  dot.vx = 0;
                                  dot.vy = 0;
                                  addChild(dot);
                                  dots.push(dot);
                          }
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);
                  }
                  
                  private function onEnterFrame(event:Event):void
                  {
                          for(var i:uint = 0; i < numDots; i++)
                          {
                                  var dot:animation_ch19_Ball = dots[i];
                                  dot.vx += Math.random() * 0.2 - 0.1;
                                  dot.vy += Math.random() * 0.2 - 0.1;
                                  dot.x += dot.vx;
                                  dot.y += dot.vy;
                                  dot.vx *= friction;
                                  dot.vy *= friction;
                                  
                                  if(dot.x > stage.stageWidth)
                                  {
                                          dot.x = 0;
                                  }
                                  else if(dot.x < 0)
                                  {
                                          dot.x = stage.stageWidth;
                                  }
                                  if(dot.y > stage.stageHeight)
                                  {
                                          dot.y = 0;
                                  }
                                  else if(dot.y < 0)
                                  {
                                          dot.y = stage.stageHeight;
                                  }
                          }
                  }
          }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.