6.8.8. Method alignCannon

Method alignCannon (Fig. 6.14) aims the cannon at the point where the user touched the screen. Line 335 gets the x- and y-coordinates of the touch from the MotionEvent argument. We compute the vertical distance of the touch from the center of the screen. If this is not zero, we calculate cannon barrel’s angle from the horizontal (line 345). If the touch is on the lower-half of the screen we adjust the angle by Math.PI (line 349). We then use the cannonLength and the angle to determine the x- and y-coordinate values for the endpoint of the cannon’s barrel—this is used to draw a line from the cannon base’s center at the left edge of the screen to the cannon’s barrel endpoint.

331   // aligns ...

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.