class SimpleBall2 extends MovieClip { // variables, all private - internally handled var boundries:Object; var velocity_x:Number; var velocity_y:Number; // constructor function SimpleBall2() { 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 ( pBoundries:Object) { //{left:10, ... } boundries = pBoundries; return this; } // onEnterFrame event to control bouncing private function onEnterFrame():Void { // move ball _x += velocity_x; _y += velocity_y; _rotation += Math.abs(velocity_x) + Math.abs(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); } } // reset original position and apply a new // random direction when the mouse is pressed private function onMouseDown():Void { _x = _parent._xmouse; _y = _parent._ymouse; velocity_x =-Math.random()*20; velocity_y = -Math.random()*20; } }