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

Ready aim fire


Now, we can give our hero a gun, and later, we can give him enemies to shoot at. We will create a MachineGun class to do all the work and a Bullet class to represent the projectiles that it fires. The Player class will control the MachineGun class, and the MachineGun class will control and keep track of all the Bullet objects that it fires.

Create a new Java class and call it Bullet. Bullets are not complicated. Ours will need a x and y location, a horizontal velocity, and a direction to help calculate the velocity.

This implies the following simple class, constructor, and a bunch of getters and setters:

public class Bullet  {

    private float x;
    private float y;
    private float xVelocity;
    private int direction;

    Bullet(float x, float y, int speed, int direction){
        this.direction = direction;
        this.x = x;
        this.y = y;
        this.xVelocity = speed * direction;
    }

    public int getDirection(){
        return direction;
    }

    public...