Now, we need to introduce some code to the ball. Take a look to the code, i will explain later.

restitution = 0.7; resistence = 0.997; acceleration_x = 0; acceleration_y = 0; gravity = 0.2; particleCount = 0; this.onEnterFrame = function() { generateParticles(); this.swapDepths(_parent.getNextHighestDepth()) acceleration_y += gravity; acceleration_y *= resistence; acceleration_x *= resistence; checkColision(); _x += acceleration_x; _y += acceleration_y; }; function accelerate(x, y) { acceleration_x += x; acceleration_y += y; } function checkColision() { if (_x+acceleration_x+(_width/2)>=_parent.rightLimit) { if (_x+(_width/2)<_parent.rightLimit) { _x = _parent.rightLimit-(_width/2)-0.1; } else { acceleration_x *= -1*restitution; } } if (_x+acceleration_x-(_width/2)<=_parent.leftLimit) { if (_x-(_width/2)>_parent.leftLimit) { _x = _parent.leftLimit+(_width/2)+0.1; } else { acceleration_x *= -1*restitution; } } if (_y+acceleration_y+(_width/2)>=_parent.bottomLimit) { if (_y+(_width/2)<_parent.bottomLimit) { _y = _parent.bottomLimit-(_width/2)-0.1; } else { acceleration_y *= -1*restitution; } } if (_y+acceleration_y-(_width/2)<=_parent.topLimit) { if (_y-(_width/2)>_parent.topLimit) { _y = _parent.topLimit+(_width/2)+0.1; } else { acceleration_y *= -1*restitution; } } } function generateParticles() { _parent.attachMovie("circleParticle","particle"+particleCount,_parent.getNextHighestDepth(),{_x:_x, _y:_y}); particleCount++; }

This code will be in the only frame we have inside the ball.
First we need to define some physic properties: restitution is the amount of energy that the ball loses after a collision. Resistence, is basically the aerodinamic resistence. Gravity will be a constant that we are going to add to the acceleration in the Y axis. Then we have the initial acceleration in both axis. Finally particleCount is just to know how many particles the ball create.
Since we want to update the ball every frame, we use the enterFrame function.
Inside we have, the particle generation function, the depth swap, to see the ball always on top of the particles. We also add the gravity to the acceleration on axis Y. The important thing here is the collision. We want to check if the ball will collide with the limits before we move it. Cos that, we call the checkCollision function, then we add the acceleration to the _x and _y properties.

CheckCollision function, have a litle workarround. Main idea is to know if the ball plus the acceleration on each axis, will collide with the limits. Anyway on most examples arround this is a simple check, and is common to see the ball colliding even if it is not near to the limit. So, first check plus acceleration, then instead of reversing ball acceleration, we check if the ball is really colliding, if not, we place the ball in the limit, next time we just apply a -1 multiplier and the restitution factor to reduce the bouncing.

Finally we move the ball. Note that the limits vars are not yet declared. We want to have it in the _root, so if we create more balls, all will use the same vars.

We also have another function: accelerate(). This function is basically to add acceleration in both axis. We are going to use this function to move the ball in the next section.

The last function is the particle generation. Everytime is called, we attach a particle movie in the _root, in the same _X and _Y than the ball have.

Ok, time to start adding interaction!