(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;
}
}