topical media & game development

talk show tell print

object-ax-bounce.ax

object-ax-bounce.ax (swf ) [ flash ] flex


  package
  {
          import flash.display.DisplayObject;
          import flash.display.Sprite;
          import flash.display.StageAlign;
          import flash.display.StageScaleMode;
          import flash.events.Event;
          
          public class @ax-object-ax-bounce extends Sprite
          {
                  private var ball:object_ax_ball;
                  private var vx:Number;
                  private var vy:Number;
                  private var bounce:Number = -0.95;
                  private var gravity:Number = 0.4;
                  private var display:DisplayObject;
                  
                  public function @ax-object-ax-bounce(d:DisplayObject)
                  {
                          display = d;
                          init();
                  }
                  
                  private function init():void
                  {
                          //stage.scaleMode = StageScaleMode.NO_SCALE;
                          //stage.align=StageAlign.TOP_LEFT;
  
                          ball = new object_ax_ball(40,0xFFFFFF);
                          ball.x = display.width / 2;
                          ball.y = display.height / 2;
                          vx = Math.random() * 20 - 10;
                          vy = -20;
                          addChild(ball);
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);
                  }
                  
                  private function onEnterFrame(event:Event):void
                  {
                          var ax:Number = Math.random() * 2 - 1;
                          var ay:Number = Math.random() * 4 - 2;
                          vx += ax;
                          vy += ay + gravity;
                          ball.x += vx;
                          ball.y += vy;
                          var sc:Number = display.width/320.0;
                          ball.scaleX = ball.scaleY = sc;
                          var left:Number = 0;
                          var right:Number = display.width;
                          var top:Number = 0;
                          var bottom:Number = display.height;
                          
                          if(ball.x + ball.radius > right)
                          {
                                  ball.x = right - ball.radius;
                                  vx *= bounce;
                          }
                          else if(ball.x - ball.radius < left)
                          {
                                  ball.x = left + ball.radius;
                                  vx *= bounce;
                          }
                          if(ball.y + ball.radius > bottom)
                          {
                                  ball.y = bottom - ball.radius;
                                  vy *= bounce;
                          }
                          else if(ball.y - ball.radius < top)
                          {
                                  ball.y = top + ball.radius;
                                  vy *= bounce;
                          }
                  }
          }
  }
  


(C) Æliens 20/2/2008

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.