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

Building the enemies


Now that we have the tap controls implemented, it is time to add some enemies that the player can boost to avoid.

This is going to be much easier than when we added our player's spaceship because most of what we need is in place already. All we have to do is code a class to represent our enemy, instantiate as many enemy objects as we need, call their update methods, and then draw them.

As we will see, the update method for our enemy will be quite different to that of PlayerShip. It will need to handle things like simple AI to fly toward the player. It will also need to handle respawning when it leaves the screen.

Designing the enemy

To begin with, create a new Java class and call it EnemyShip. Add these member variables inside the class so your new class will look like this:

public class EnemyShip{
    private Bitmap bitmap;
    private int x, y;
    private int speed = 1;

    // Detect enemies leaving the screen
    private int maxX;
    private int minX;

    // Spawn...