topical media & game development

talk show tell print

#try-toss-script.ax

#try-toss-script.ax (swf ) [ flash ] flex


  package {
  
  
www.senocular.com/flash/tutorials/as3withmxmlc

import mx.core.UIComponent; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; import flash.geom.Rectangle; public class @ax-try-toss-script extends Sprite { private var ball:TossableBall; private var lastMouse:Point = new Point(); private var mouseMoved:Point = new Point(); private var w:Number = 800; private var h:Number = 600; public function @ax-try-toss-script() { //var bounds:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight); var bounds:Rectangle = new Rectangle(0, 0, w,h); ball = new TossableBall(50, bounds); ball.x = bounds.x + bounds.width/2; ball.y = bounds.y + bounds.height/2; addChild(ball); ball.addEventListener(MouseEvent.MOUSE_DOWN, grabBall); ball.addEventListener(MouseEvent.MOUSE_UP, releaseBall); } private function grabBall(evt:MouseEvent):void { ball.toss(new Point(0,0)); ball.startDrag(); lastMouse = new Point(mouseX, mouseY); addEventListener(Event.ENTER_FRAME, moveBall); moveBall(new Event(Event.ENTER_FRAME)); } private function releaseBall(evt:MouseEvent):void { ball.stopDrag(); ball.toss(mouseMoved); removeEventListener(Event.ENTER_FRAME, moveBall); } private function moveBall(evt:Event):void { var currMouse:Point = new Point(mouseX, mouseY); mouseMoved = currMouse.subtract(lastMouse); lastMouse = currMouse; } } }

(private) classes


  // only visible within file
  
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.geom.Point;
  import flash.geom.Rectangle;
  
  class TossableBall extends Sprite {
          
          private var bounds:Rectangle;
          private var vector:Point = new Point();
          private var friction:Number = .95;
                  
          public function TossableBall(size:Number, throwBounds:Rectangle) {
                  bounds = throwBounds;
                  graphics.lineStyle(1);
                  graphics.beginFill(0xFF8000);
                  graphics.drawCircle(0, 0, size/2);
                  
                  addEventListener(Event.ENTER_FRAME, soar);
          }
          
          public function toss(tossVector:Point):void {
                  vector = tossVector;
          }
          
          private function soar(evt:Event):void {
                  x += vector.x;
                  y += vector.y;
                  var shapeBounds:Rectangle = getBounds(parent);
                  if (shapeBounds.left < bounds.left) {
                          vector.x = Math.abs(vector.x);
                  }else if (shapeBounds.right > bounds.right) {
                          vector.x = -Math.abs(vector.x);
                  }
                  if (shapeBounds.top < bounds.top) {
                          vector.y = Math.abs(vector.y);
                  }else if (shapeBounds.bottom > bounds.bottom) {
                          vector.y = -Math.abs(vector.y);
                  }
                  vector.x *= friction;
                  vector.y *= friction;
          }
  }
  


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