我的游戏循环没有绘制生成对象

时间:2019-01-15 21:10:00

标签: java android loops

问题在于生成的小行星没有出现在GameScreen上。似乎他们甚至都没有创造。我创建了World类,该类管理小行星(它们的生成和销毁),并将其传递给GameScreen。当我直接在GameScreen的updateRunning()方法中调用方法world.spawnAsteroids时,它们会显示在屏幕上,但是在非常快地生成约3-5个小行星后,游戏便崩溃了。我不知道是什么问题。

 (...)

 //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);
}

}

////游戏屏幕类

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 个答案:

答案 0 :(得分:1)

您的游戏似乎以

开头
death=false

因此,您的小行星不会产生,因为会跳过该循环 您应该将while循环更改为:

while (!death)

答案 1 :(得分:0)

非常感谢您,塔克拉底。我做了您建议的更改,但现在循环开始时游戏崩溃。在日志中,我发现错误提示小行星的位图为空。我的小行星类的构造函数是这样的:(Assets。(...)只是Assets类中的图像实例)

    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;
}