topical media & game development

talk show tell print

animation-ch09-Boxes.ax

animation-ch09-Boxes.ax [swf] [flash] flex


  package
  {
          import flash.display.Sprite;
          import flash.events.Event;
          
          public class @ax-animation-ch09-Boxes extends Sprite
          {
                  private var box:animation_ch09_Box;
                  private var boxes:Array;
                  private var gravity:Number = 0.2;
                  
                  public function @ax-animation-ch09-Boxes()
                  {
                          init();
                  }
                  
                  private function init():void
                  {
                          boxes = new Array();
                          createBox();
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);
                  }
                  
                  private function onEnterFrame(event:Event):void
                  {
                          box.vy += gravity;
                          box.y += box.vy
                          if(box.y + box.height / 2 > stage.stageHeight)
                          {
                                  box.y = stage.stageHeight - box.height / 2;
                                  createBox();
                          }
                          for(var i:uint = 0; i < boxes.length; i++)
                          {
                                  if(box != boxes[i] && box.hitTestObject(boxes[i]))
                                  {
                                          box.y = boxes[i].y - boxes[i].height / 2 - box.height / 2;
                                          createBox();
                                  }
                          }
                  }
                  
                  private function createBox():void
                  {
                          box = new animation_ch09_Box(Math.random() * 40 + 10, Math.random() * 40 + 10);
                          box.x = Math.random() * stage.stageWidth;
                          addChild(box);
                          boxes.push(box);
                  }
          }
  }
  


(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.