class Bouncer3 extends MovieClip { // variables, all private - internally handled var gravity:Number = 0; var boundries:Object; var velocity_x:Number; var velocity_y:Number; var velocity_r:Number; var original_x:Number; var original_y:Number; var dampfactor:Number = 0; // constructor function Bouncer3() { 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, velx:Number, vely:Number, velr:Number) { gravity = pGravity; dampfactor = pDampfactor; boundries = pBoundries; velocity_x = velx; velocity_y = vely; velocity_r = velr; return this; } // onEnterFrame event to control bouncing private function onEnterFrame():Void { // move ball _x += velocity_x; _y += velocity_y; _rotation += velocity_r; // check for boundry collision if (_x <= boundries.left) { _x = boundries.left; velocity_x = Math.abs(velocity_x); velocity_y *= dampfactor; velocity_x *= dampfactor; velocity_r *=dampfactor; }else if (_x >= boundries.right) { _x = boundries.right; velocity_x = -Math.abs(velocity_x); velocity_y *= dampfactor; velocity_x *= dampfactor; velocity_r *=dampfactor; } if (_y <= boundries.top) { _y = boundries.top; velocity_y = Math.abs(velocity_y); velocity_y *= dampfactor; velocity_x *= dampfactor; velocity_r *=dampfactor; }else if (_y >= boundries.bottom) { _y = boundries.bottom; velocity_y = -Math.abs(velocity_y); velocity_y *= dampfactor; velocity_x *= dampfactor; velocity_r *=dampfactor; } // apply gravity velocity_y += gravity; } // 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; velocity_r = Math.random()*20; velocity_y = -Math.random()*20;*/ } }