Bounce Multiple Objects

if statement

Open balls_wrap and save as balls_bounce

We're going to get these balls moving at different velocities. How? Each ball object will hold it's own speed variable, separate and independent from all other ball objects. By using dot syntax, we'll just set a speed variable inside myBall like this:

myBall.speed = (Math.random()*5)+5;

This is just slightly different than the last time we used Math.random. The first part of our equation is familiar: Math.random()*5 will generate a random number between 0 and 4. Since I don't want any balls to just sit there, myBall.speed shouldn't ever equal 0. So, we add 5 to that product. Now we'll get a number between 5 and 9.

Once we have myBall.speed set, we'll use it to set the Y position in our moveBall function:

Check it out... the balls should now move like this:

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player