6.8.7. Method fireCannonball

When the user touches the screen, method onTouchEvent (Section 6.8.13) calls fireCannonball (Fig. 6.13). If there’s already a cannonball on the screen, the method returns immediately. Line 313 calls alignCannon to aim the cannon at the touch point and get the cannon’s angle. Lines 316–317 “load the cannon” (that is, position the cannonball inside the cannon). Then, lines 320 and 323 calculate the horizontal and vertical components of the cannonball’s velocity. Next, we set cannonballOnScreen to true so that the cannonball will be drawn by method drawGameElements (Fig. 6.15) and increment shotsFired. Finally, we play the cannon’s firing sound (represented by the CANNON_SOUND_ID).

307 ...

Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.