My game loop doesn't draw spawning objects

Viewed 41

The problem is that spawning asteroids doesn't show up on GameScreen. It seems like they aren't even creating. I created World class, which manage asteroids (their spawning and destroying) and passed it to GameScreen. When I call method world.spawnAsteroids directly in the GameScreen updateRunning() method they show up on screen, but then game crashes after very fast spawned about 3-5 asteroids. I have no idea what is the problem.

 (...)

 //world which manage asteroids
 public class World  {

//maximum y coordination for asteroids
static final int deathY = 890;
static final float TICK_INITIAL = 0.5f;
static final int screenWidth = 720;

float tickTime = 0;
float tick = TICK_INITIAL;

static final float TICK_DECREMENT = 0.05f;

public boolean gameOver = false;
public int actualScore = 0;
Random random = new Random();

List<Asteroid> asteroids;

//if asteroid go deathY
public boolean death = false;

public World(){
    asteroids = new ArrayList<Asteroid>();
}

public void update(float deltaTime) {
    if (gameOver)
        return;

    tickTime += deltaTime;

    while (death) {
        tickTime -= tick;

        //spawning new asteroid
        spawnAsteroid();

        //all already created asteroids 
        for(Asteroid as : asteroids){
            as.move();

            //if any asteroid reaches max y
            if(as.actualY >= deathY - as.asteroidIMG.getHeight()){
                gameOver = true;
                return;
            } 
        }
    }
}


//spawning asteroids method
public void spawnAsteroid(){
    Random random = new Random();

    //always on top of screen
    int y = 0;

    //random start x
    int x = random.nextInt(720);

    //random size of asteroid
    int asteroidSize = random.nextInt(2);

    //random color
    int color = random.nextInt(5);

    //create new asteroid and add to list of all asteroids
    Asteroid as = new Asteroid(asteroidSize, color, x, y);
    asteroids.add(as);

     //move asteroid along y
    as.move();
}

//delete asteroid method (asteroid is deleting after tapping on it)
private void destroyAsteroid(Asteroid asteroid){
    asteroid.asteroidIMG.dispose();
    asteroids.remove(asteroid);
}

}

///game screen class

public class GameScreen extends Screen {
//game states
enum GameState {
    Ready,
    Running,
    Paused,
    GameOver
}

AndroidFastRenderView gameView;

static final int deathY = 890;

GameState state = GameState.Ready;

//world instance
World world;

int scoreInt = 0;
String score = "0";

//list of asteroids
List<Asteroid> asteroids = new ArrayList<Asteroid>();

public GameScreen(Game game){
    super(game);
    world = new World();
    asteroids = world.asteroids;
}

(...)

//metoda update w stanie running
private void updateRunning(List <TouchEvent> touchEvents, float deltaTime){
    //ile jest eventow dotyku
    int len = touchEvents.size();


    for(int i = 0; i < len; i++){
        TouchEvent event = touchEvents.get(i);

        if(event.type == TouchEvent.TOUCH_UP) {
            if(event.x > 589 && event.x < 689 && event.y > 1172) {

                if(Settings.soundEnabled)
                    Assets.click.play(1);


                state = GameState.Paused;
                return;
            }
        }

        if(event.type == TouchEvent.TOUCH_DOWN){
            if(asteroids.size() > 0){
                for(Asteroid as : asteroids){
                    //if asteroid gots clicked
                    if(event.x > as.actualX && event.y > as.actualY
                            && event.x < (as.actualX + 
                              as.asteroidIMG.getWidth())
                            && event.y < (as.actualY + 
                              as.asteroidIMG.getHeight())){

                        Assets.destroyAsteroid.play(1);
                        scoreInt += as.score;
                    }
                }
            }
        }
    }

    world.update(deltaTime);

    if(world.gameOver) {
        if(Settings.soundEnabled)
            Assets.gameOver.play(1);
        state = GameState.GameOver;
    }

    //score update
    if(scoreInt != world.actualScore) {
        scoreInt = world.actualScore;
        score = "" + scoreInt;
    }
}

//method of drawing asteroids to screen
private void drawRunning(){
    (...)

    if(asteroids.size() > 0){
        for(Asteroid as : asteroids){
            //drawing asteroid
             //(draw Pixmap is my own method inside framework)
            g.drawPixmap(as.asteroidIMG, as.actualX, as.actualY);
        }
    }

    //drawing text score
    g.drawText(328, 1229, score);
}         

}

2 Answers

It appears that your game starts with

death=false

And therefore your asteroids are never spawned because that looped is skipped You should probably change that while loops to:

while (!death)

Thank you so much, Tacratis. I did changes you suggested, but now game crashes on loops start. In logs I found error says that asteroid's bitmaps are nulls. Constructor for my asteroid class is like this: (Assets.(...) is just image instance in Assets class)

    public Asteroid(int size, int color, int x, int y){
    this.size = size;

    if(size == 0){
        this.speed = 10;
    } else {
        this.speed = 7;
    }

    this.color = color;

    //set image of asteroid regards to size and color
    if(this.size == 0 && this.color == 0){ 
        asteroidIMG = Assets.smallAsteroidBlue;
    } else if (this.size == 0 && this.color == 1){  
        asteroidIMG = Assets.smallAsteroidGreen;
    } else if (this.size == 0 && this.color == 2){ 
        asteroidIMG = Assets.smallAsteroidRed;
    } else if (this.size == 0 && this.color == 3){ 
        asteroidIMG = Assets.smallAsteroidYellow;
    } else if (this.size == 1 && this.color == 0){ 
        asteroidIMG = Assets.bigAsteroidBlue;
    } else if (this.size == 1 && this.color == 1){ 
        asteroidIMG = Assets.bigAsteroidGreen;
    } else if (this.size == 1 && this.color == 2){ 
        asteroidIMG = Assets.bigAsteroidRed;
    } else if (this.size == 1 && this.color == 3){
        asteroidIMG = Assets.bigAsteroidYellow;
    }

    this.actualX = x;
    this.actualY = y;
}
Related