Game Development Reference
In-Depth Information
7.
Save the script.
8. Click Play, and test the new reward system.
At some point, you may notice that things have gotten out of hand (Figure 9-1 ). The loadRate has
dropped below 0.1, the potatoes seem to be hitting each other on the way out, and the zombie
bunny count is off, with more reported kills than there were zombie bunnies.
Figure 9-1. Excessive explosions and an erroneous zombie bunny count
So what is wrong? The most likely culprit is frame rate. In this scenario, where you can have multiple
hits very close together, the calculations may not always get a chance to finish. With the zombie
bunnies, because there is a delay after the hit before they are destroyed, they could be “hit” more
than once. For that, you can introduce a flag to track the first hit.
9.
In the ReceivedHit script, add the following:
bool alreadyDead = false; // flag to prevent duplicate 'deaths'
10.
At the top of the DestroyBun function, add the following:
if (alreadyDead) return; // bypass the rest of the function
alreadyDead = true; // set the flag
Here's what is happening: The alreadyDead flag is false the first time through, so the if clause is not
activated. The flag, because this is the first time through, is now set to true and the rest of the code
is carried out. Now, if a second strike happens before the zombie bunny is removed from the scene,
the if clause catches it and tells it to return , or exit the function immediately.
11.
Save the script and test.
Search WWH ::




Custom Search