Kabomb > Tutorials > Intermediate Movieclip Control
Introduction
This is a more realistic ball control with a much more fluid ball movement.
The Code
Create a circle and convert it to a MC with an instance name of 'ball'.

On the main timeline add this code:

Explanation
The variables you set are xspeed and yspeed (horizontal and vertical speeds), acceleration (how fast the speeds will speed up), friction (how slow the speeds will slow down when no key are down), maxspeed (maximum speed), and radius (half the ball's height or diametre).

In the onEnterFrame function, you first check which keys are down. If the left key is down, xspeed will have acceleration subtracted from it. This means the longer the left key is held down, the xspeed will be more and more negative. It's the same for a car- the longer you hold down the accelerator, your speed will be more and more. You should use 'else' to check if the right key is down so you can also tell when none of them are down. If the right key is down, xspeed will have acceleration added to it, so it becomes more and more positive. If none of these keys are down, xspeed will have friction multiplied by it. If you multiply any number by something greater than 0 but less than 1, it will go closer to 0 every time you do it. See it here:

10x0.9 = 9 x0.9 = 8.1 x0.9 = 7.29 x0.9 = 6.561 and so on. Also:
-10x0.9 = -9 x0.9 = -8.1 x0.9 = -7.29 x0.9 = -6.561 and so on.

So if the left and right keys aren't down, xspeed will go closer to 0 (it slows down to a stop).
You duplicate this code for the y values.

Next, you check if the xspeed goes greater than maxspeed (if it does, let xspeed=maxspeed) or less than negative maxspeed (then let xspeed=-maxspeed). Same for the y values.

Then you check if the ball hits the boundaries. You use the stage's width (the very right of screen) as the boundary, but you then need to go into the stage (or subtract) the radius of the ball. This is because the centre of the ball is where it's x position is, so you take off radius to make the boundary smaller and the x position cannot go greater than that. If you didn't take the radius away, this would happen:
(|
as opposed to this:
()|

If the ball does breach this boundary, you set the xspeed to negative. Firstly, you find the absolute value (makes it positive) and then make this negative. This ensures the xspeed will alway be negative no matter what. Then, you check if the ball's x is less than radius (0+radius or the very left of screen + radius). If it is, you set xspeed it it's absolute value. Again, you use the same code for y values.

The last two lines set the balls x and y positions in relation to xspeed, after they had been altered considering what happened on the frame. The xspeed is added onto the ball's x position, like a cars speed is added to it's distance every interval. So if a car was travelling at 30 m/s, the distance travelled after 1s is 30 m. Then after the next second, the distance travelled is 30 + speed = 60m. Likewise, you add the xspeed onto the x position.

Conclusion
Test the movie and use the arrow keys to control.