Game Development Reference
In-Depth Information
Clicking the left mouse button fires the bullets:
lner]pabqj_pekjkjIkqoa@ksj$arajp6IkqoaArajp%6rke`
w
eb$[odkpBena`%
w
odkkp>qhhap$%7
[odkpBena`9pnqa7
y
y
lner]pabqj_pekjkjIkqoaQl$arajp6IkqoaArajp%6rke`
w
[odkpBena`9b]hoa7
y
The [odkpBena` variable keeps track of whether a shot has been fired. This prevents a bullet from being
fired more than once each time the left mouse button is clicked. You could actually have left out this check,
and the code would have worked pretty well, but occasionally a mouse click results in more than one bullet
being fired. Using a Boolean variable to limit bullets to one per mouse click is a fail-safe system.
The odkkp>qhhap method actually does the work of firing the bullet:
lner]pabqj_pekjodkkp>qhhap$%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]`eqo6ejp9)1,7
r]n^qhhap[Op]npT6Jqi^an9t'n]`eqo&I]pd*_ko$[]jcha%7
r]n^qhhap[Op]npU6Jqi^an9u'n]`eqo&I]pd*oej$[]jcha%7
++?na]pa^qhhapejop]j_a]j`]``eppkpdaop]ca
r]n^qhhap6>qhhap9jas>qhhap$^qhhap[Rt(^qhhap[Ru(^qhhap[Op]npT( ±
^qhhap[Op]npU(op]n%7
l]najp*]``?deh`$^qhhap%7
y
First, its velocity is calculated:
r]n^qhhap[Rt6Jqi^an9I]pd*_ko$[]jcha%&)-,7
r]n^qhhap[Ru6Jqi^an9I]pd*oej$[]jcha%&)-,7
These directives reuse the []jcha variable that you calculated earlier. I]pd*_ko and I]pd*oej are used
to find the ratio of two sides of the triangle formed by the angle, the same way you used them to help
move Button Fairy. These numbers are then multiplied by -10, which is the effective velocity of the
bullets: 10 pixels per frame. These numbers need to be negative so the bullets move in the right direc-
tion. The results of these calculations are stored in ^qhhap[Rt and ^qhhap[Ru. You'll soon be passing
these variables to the >qhhap class, which uses this information to help make the bullets.
 
Search Nedrilad ::




Custom Search