O'Reilly logo

Android Game Programming by Example by John Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required