Reader small image

You're reading from  Android Game Programming by Example

Product typeBook
Published inJun 2015
Reading LevelIntermediate
Publisher
ISBN-139781785280122
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

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();
 ...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Android Game Programming by Example
Published in: Jun 2015Publisher: ISBN-13: 9781785280122

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton