Timing is very important in creating realistic animations in Greenfoot. Often, we have the need for an actor to do temporary animation in response to an event. We need a way to allow (or prevent) things for a certain amount of time. It is possible to wait for a specific amount of time using the SimpleTimer
class provided by Greenfoot (you can import it into your scenario in the same way you imported the Counter
class in Chapter 1, Let's Dive Right in…); however, waiting for a specific amount of time is rarely the right choice.
Why is that? Well, Greenfoot provides the player/user with the ability to slow down and speed up a scenario via the Speed slider that is located at the bottom of Greenfoot's main scenario window. If you waited for 2 seconds in your code and then the player sped the game up, the 2 seconds wait would last much longer in the game relative to the speed of everything else; the reverse effect would happen if the user slowed down the scenario. We...