topical media & game development

talk show tell print

lib-flex-animation-code-06-VerletPoint.ax

lib-flex-animation-code-06-VerletPoint.ax (swf ) [ flash ] flex


  package
  {
          import flash.display.Graphics;
          import flash.geom.Rectangle;
          
          public class @ax-lib-flex-animation-code-06-VerletPoint
          {
                  public var x:Number;
                  public var y:Number;
                  
                  private var _oldX:Number;
                  private var _oldY:Number;
                  
                  
                  public function @ax-lib-flex-animation-code-06-VerletPoint(x:Number, y:Number)
                  {
                          setPosition(x, y);
                  }
                  
                  public function update():void
                  {
                          var tempX:Number = x;
                          var tempY:Number = y;
                          x += vx;
                          y += vy;
                          _oldX = tempX;
                          _oldY = tempY;
                  }
                  
                  public function setPosition(x:Number, y:Number):void
                  {
                          this.x = _oldX = x;
                          this.y = _oldY = y;
                  }
                  
                  public function constrain(rect:Rectangle):void
                  {
                          x = Math.max(rect.left, Math.min(rect.right, x));
                          y = Math.max(rect.top, Math.min(rect.bottom, y));
                  }
                  
                  public function set vx(value:Number):void
                  {
                          _oldX = x - value;
                  }
                  public function get vx():Number
                  {
                          return x - _oldX;
                  }
                  
                  public function set vy(value:Number):void
                  {
                          _oldY = y - value;
                  }
                  public function get vy():Number
                  {
                          return y - _oldY;
                  }
                  
                  public function render(g:Graphics):void
                  {
                          g.beginFill(0);
                          g.drawCircle(x, y, 4);
                          g.endFill();
                  }
  
          }
  }


(C) Æliens 18/6/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.