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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
7
Platformer – Guns, Life, Money, and the Enemy
Index

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
        lm.player.setxVelocity(0);
        lm.player.setPressingRight(false);
    }

   if (hit == 2) {// Feet
        lm.player.isFalling...