Bouncing Ball!

Cleanup with Math

We have a working program, but have set ourselves up for a hassle if we want to change the speed of our ball. Our value shows up three times, and at different places, in our code:

We don't want to hunt down those 2's every time we decide to make a change. There's a better trick!

Challenge: How do you turn a negative number into a positive?

Hint: It's the same way we turn a positive into a negative.

......have you figured it out?

If you guessed: Multiply by a negative number... you're right! All we have to do is multiply speed by -1 and the value of speed just keeps flipping between positive and negative!

We can use:

speed = speed*(-1)

...or we can take a programming shortcut similar to the shortcut we took before:

Test, test, test:

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

Get Adobe Flash player