Fixing the Final Bug
In many commercial software development projects, most of the time and money is spent testing programs to make sure they work as expected, and then fixing them when they don’t. Errors in programs are often called bugs, and even in our simple game here, we have one that would enable the player to cheat.
If the fireball is moving up the screen and the player presses the Fire key again, the firing sequence starts over. That means the fireball that was travelling through the air disappears, and a new one is sent up from the ship. That doesn’t make any logical sense, and it means players suffer no consequences if they misfire: They can just fire again and it’s as if the misfired shot never happened.
We can use a variable to keep note of when the fireball is moving up the screen so that we can stop the ship from allowing a fireball to be fired again at that time. Variables like this, which are just used to keep track of whether something is happening, are called flags. Our firing flag needs to be able to say whether the fireball is in play or not, so it has two values. While the fireball is on screen, we give the firing flag a value of
1. When it isn’t, the firing flag has a value of
Click the Variables button at the top of the Blocks Palette, and click the option to make a variable. Give it the name
firingflag and make sure the button is selected so it is available for all sprites.
After you’ve created the variable, you can drag a block in from the Variables ...