class Bouncer1 extends MovieClip { // variables, all private - internally handled var gravity:Number = 0; var boundries:Object; var velocity_x:Number; var velocity_y:Number; var original_x:Number; var original_y:Number; var dampfactor:Number = 0; // constructor function Bouncer1() { original_x = _x; original_y = _y; Mouse.addListener(this); onMouseDown(); // force mouseDown event } // init method for arguments which in other normal // circumstances (non-MovieClip classes) would be // given to the constructor. Not an option with MovieClips function init (pGravity:Number, pDampfactor:Number, pBoundries:Object) { gravity = pGravity; dampfactor = pDampfactor; boundries = pBoundries; return this; } // onEnterFrame event to control bouncing private function onEnterFrame():Void { // move ball _x += velocity_x; _y += velocity_y; // check for boundry collision if (_x <= boundries.left) { _x = boundries.left; velocity_x = Math.abs(velocity_x); }else if (_x >= boundries.right) { _x = boundries.right; velocity_x = -Math.abs(velocity_x); } if (_y <= boundries.top) { _y = boundries.top; velocity_y = Math.abs(velocity_y); }else if (_y >= boundries.bottom) { _y = boundries.bottom; velocity_y = -Math.abs(velocity_y); } // apply gravity velocity_y += gravity; velocity_y *= dampfactor; velocity_x *= dampfactor; } // reset original position and apply a new // random direction when the mouse is pressed private function onMouseDown():Void { _x = original_x; _y = original_y; velocity_x = Math.random()*20-10; velocity_y = -Math.random()*20; } }