Game Development Reference
In-Depth Information
Finally, you can use the PeianArajp class's PEIAN[?KILHAPA property to trigger an event when the
timer finishes:
[peian*]``ArajpHeopajan$PeianArajp*PEIAN[?KILHAPA(kjPeian?kilhapa%7
This is a very basic introduction to how timers work, but I'm sure you can see that they are used often
in games. You can use a Peian object to create a countdown timer, make an object mover intermit-
tently, or calculate a player's score based on how long it takes to complete a task.
Using a timer to fire bullets
Now that you know how a timer works, it should be quite obvious how one has been used in the
Nk^kp[Odkkp class. The [peian object has been set up to trigger an event every second. However, it's
not started when the class is initialized. Instead, it waits until the target object is within range (which
is 250 pixels).
eb$`eop]j_a89N=JCA%
w
[peian*op]np$%7
y
ahoa
w
[peian*opkl$%7
y
The event that the timer calls is the kjOdkkp>qhhap bullet event. It's identical to the kjOdkkp>qhhap
method from the Lh]uan class, except that _en_ha is provided as an argument instead of op]n. It's
no more difficult than that!
Shooting at random intervals
There's one small modification you can make that will make the robot's shooting behavior much more
realistic. As it's been coded, the robot shoots right on cue every 1000 milliseconds. You can use the
Peian class's `ah]u property to randomize this. Add the following code to the kjOdkkp>qhhap event
handler to see the effect:
lner]pabqj_pekjkjOdkkp>qhhap$arajp6PeianArajp%6rke`
w
++>qhhap#orahk_epu
r]n^qhhap[Rt6Jqi^an9I]pd*_ko$[]jcha%&)-,7
r]n^qhhap[Ru6Jqi^an9I]pd*oej$[]jcha%&)-,7
++>qhhap#oop]nplkoepekj
r]nn]`eqo9)1,7
r]n^qhhap[Op]npT9t'n]`eqo&I]pd*_ko$[]jcha%7
r]n^qhhap[Op]npU9u'n]`eqo&I]pd*oej$[]jcha%7
 
Search Nedrilad ::




Custom Search