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

Twinkling stars

We will get a bit more mobile than a static border. Here, we will add an update method to a simple Star class, which can be used to randomly switch the star on and off.

We set the type as normal and create a random location for the star within the confines of the border and call setWorldLocation() as always.

Stars will be drawn as points, so our vertex array will simply contain one vertex at model space 0,0,0. Then, we call setVertices() as usual.

Create a new class, call it Star, and enter the discussed code:

public class Star extends GameObject{

    // Declare a random object here because
    // we will use it in the update() method
    // and we don't want GC to have to keep clearing it up
    Random r;

    public Star(int mapWidth, int mapHeight){
    r = new Random();

    // Define the star
    // as a single point
    // in exactly the coordinates as its world location