Book Image

Android Game Programming by Example

By : John Horton
Book Image

Android Game Programming by Example

By: John Horton

Overview of this book

Table of Contents (18 chapters)
Android Game Programming by Example
About the Author
About the Reviewers
Platformer – Guns, Life, Money, and the Enemy

Bullet collision detection

Detecting bullet collisions is fairly straightforward. We loop through all the existing Bullet objects held by our MachineGun object. Next, we convert the points of each bullet into a RectHitBox object and test it using intersects() against each object in our viewport.

If we get a hit, we check to see what type of object it has hit. We then switch to handle each type of object that we care about. If it is a Guard object, we knock it back a bit, if it is a Drone object, we destroy it, and if it is anything else, we just make the bullet disappear and play a kind of thudding/ricochet sound.

We simply place this logic we discussed after our switch block that handles collisions with the player, but before, we call update() on all our unclipped objects as shown next:

default:// Probably a regular tile
    if (hit == 1) {// Left or right

   if (hit == 2) {// Feet