package { import flash.display.Sprite; import flash.display.Bitmap; /** * ... * @author R J Morris */ [SWF(backgroundColor='#ffffff')] public class Ball extends Sprite { public var lastx:Number; public var lasty:Number; public var xSpeed:Number; public var ySpeed:Number; public var RADIUS:Number = 6.0; public var DRAIN:Number = 0.9; public var gravity:Number = 25; [Embed(source="/Assets/ball.png")] public var ballImage:Class; public function Ball(parentSprite:Sprite) { parentSprite.addChild(this); var instanceOfBMP:Bitmap = new ballImage() as Bitmap; instanceOfBMP.x = - RADIUS * 0.5; instanceOfBMP.y = - RADIUS * 0.5; addChild( instanceOfBMP ); respawn(); } public function respawn(): void { x = 10; y = 20; lastx = x; lasty = y; xSpeed = 5; ySpeed = 0; } public function everyFrame(main:Main):Boolean { //if((x>380 && x < 410) && (y>112 && y < 142)) // trace("(" + x + "," + y + ")"); lastx = x; lasty = y; // advance ball position x += xSpeed; y += ySpeed; if (xSpeed * xSpeed + ySpeed * ySpeed < 0.05) { xSpeed = 0; ySpeed=0; return true; } // bounce off walls if(x > (Grid.WIDTH - RADIUS) && xSpeed > 0) { xSpeed *= -DRAIN; ySpeed *= DRAIN; //trace("x " + xSpeed); } else if(x < RADIUS && xSpeed < 0) { xSpeed *= -DRAIN; ySpeed *= DRAIN; //trace("x " + xSpeed); } else if (y > (Grid.HEIGHT - RADIUS) && ySpeed > 0) { // stop if too slow xSpeed *= DRAIN; ySpeed *= -DRAIN; //trace("x "+xSpeed+"y " + ySpeed); } else if(y < RADIUS && ySpeed < 0) { xSpeed *= DRAIN; ySpeed *= -DRAIN; //trace("y " + ySpeed); } else { // apply air resistance //ySpeed *= 0.99; // apply gravity ySpeed += gravity/100; } return false; // do not lose life } // end of everyFrame function } }