Game Development Reference
In-Depth Information
3. Save the I]ej[Lh]ucnkqj`*]o file and test the project. When
the player touches the enemy, you'll see the score increase in
the iaoo]ca@eolh]u text field. Hey, what's that!? It works, but
probably not the way you expected it to! Figure 7-15 shows
what happens when the two objects collide.
The score is increased, but it's increased by one 30 times every sec-
ond! If you think about it, the code did exactly what you asked it to.
Figure 7-15. The score increases
when the two objects touch.
When the two objects collide, the following directives run:
The score is increased by 1 and then displayed as a string in the iaoo]ca@eolh]u text field. But
remember that this is all happening inside the kjAjpanBn]ia event handler, which is updated 30 times
per second. That means that every time it updates, it increases the score by 1 if the objects are touch-
ing. And so you end up with this dizzying rush of flashing numbers until the two objects are separated
Sometimes this might be the way you want your games to keep score, but let's try and refine it a little
more in this example.
How about this: Let's try and set the program up so that the score is increased only on the first
occasion that the two objects collide. Even though the two objects might be touching for an entire
6 seconds, for example, the score should be updated by only 1, not 180. Sound a bit better?
Let's take it a step farther. If the two objects are separated and then touch again, the score should
register this new collision, and update by 1 again, giving you a total of 2. This seems like a much more
logical way for the score system to work.
To put this in place, you need to use a new Boolean variable. In the program, you'll give it the follow-
ing name:
Search Nedrilad ::

Custom Search