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

The spaceship

This class is nice and simple, although it will evolve with the project. The constructor receives the starting location within the game world. We set the ship's type and world location using the methods from the GameObject class, and we set a width and height.

We declare and initialize some variables to simplify the initialization of the model space coordinates, and then we go ahead and initialize a float array with three vertices that represent the triangle that is our ship. Note that the values are based around a center of x = 0 and y = 0.

All we do next is, call setVertices(), and GameObject will prepare the ByteBuffer ready for OpenGL:

public class SpaceShip extends GameObject{

  public SpaceShip(float worldLocationX, float worldLocationY){

        // Make sure we know this object is a ship
        // So the draw() method knows what type
        // of primitive to construct from the vertices