Game Development Reference
In-Depth Information
da]hpd*iapan*se`pd))7
o_kna''7
iaoo]ca@eolh]u*patp9Opnejc$o_kna%7
y
ahoa
w
ajaiu*ckpk=j`Opkl$-%7
y
++?da_gbknaj`kbc]ia
eb$da]hpd*iapan*se`pd8-%
w
iaoo]ca@eolh]u*patp9C]iaKran7
y
y
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:
o_kna''7
iaoo]ca@eolh]u*patp9Opnejc$o_kna%7
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
again.
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:
_khheoekjD]oK__qnna`
 
Search Nedrilad ::




Custom Search