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 thrill of flight – scrolling the background

Implementing our space dust is going to be really quick and easy. All we will do is create a SpaceDust class with very similar properties to our other game objects. Spawn them into the game at a random location, move them toward the player at a random speed, and respawn them on the far right of the screen, again with a random speed and y coordinate.

Then in our TDView class, we can declare a whole array of these objects, update, and draw them each frame.

Create a new class and call it SpaceDust. Now enter this code:

public class SpaceDust {

    private int x, y;
    private int speed;

    // Detect dust leaving the screen
    private int maxX;
    private int maxY;
    private int minX;
    private int minY;

    // Constructor
    public SpaceDust(int screenX, int screenY){

        maxX = screenX;
        maxY = screenY;
        minX = 0;
        minY = 0;

        // Set a speed between  0 and 9
        Random generator = new Random();