简体   繁体   中英

Drawing multiple sprites in libgdx

I am using libgdx 1.2.0 + eclipse. I want to draw multiple sprites from a class in my game screen, but only one sprite gets drawn. This is my render method

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(100/255f, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    if(Gdx.input.justTouched()){
        touchX = Gdx.input.getX();
        touchY = Gdx.input.getY();
        //for(int i=0; i < enemy.length; i++)
            //System.out.println(enemy[i].x + " " + enemy[i].y);
    }

    for(int i=0; i< enemy.length; i++){
        enemy[i].update(delta);

        if(enemy[i].getSprite().getBoundingRectangle().contains(touchX/ppX, touchY/ppY))
            enemy[i].reset();

        if(enemy[i].gameOver){
            gameOver = true;
            //System.out.println("Over");
        }

    }

    game.batch.begin();

    //if(!gameOver){
        for(int i=0; i < enemy.length; i++)
            enemy[i].getSprite().draw(game.batch);
    //}

    game.batch.end();

}

Same thing happens if I call

enemy[0].getSprite().draw(game.batch);
enemy[1].getSprite().draw(game.batch);

It always draws something if I call just one of them.

This is the Enemy class. I load the same sprite from the game screen class in every instance of Enemy, but I change it's position.

public class Enemy {

private final float minX=1, minY=2;
private final float maxX=31, maxY=18;

private float x,y;
private float time;

private Sprite sprite;

boolean gameOver;

public Enemy(Sprite sprite){
    this.sprite = sprite;
    reset();
}

public Sprite getSprite(){
    return sprite;
}

public void reset(){
    x = minX + (int)(Math.random() * (maxX - minX + 1));
    y = minY + (int)(Math.random() * (maxY - minY + 1));
    time = 0.0f;
    gameOver = false;
    sprite.setPosition(x, y);
}

public void update(float delta){
    time += delta;
    if(time >= 3.0f)
        gameOver = true;
}

}

I load the same sprite from the game screen class in every instance of Enemy, but I change it's position.

It's the problem, you must create a new Sprite() for each Enemy object. However, when you change the sprite position of an enemy, the position of all the enemies are changed.

副作用

As you can see, you will reach the same sprite doing enemy[0].getSprite() or enemy[1].getSprite() . You can easily check this with the reference equality enemy[0].getSprite()==enemy[1].getSprite() .

You can solve this issue with something like

public Enemy(Sprite sprite){
    this.sprite = new Sprite(sprite);
    reset();
}

Another strange thing in your code :

public void reset(){
    x = minX + (int)(Math.random() * (maxX - minX + 1));
    y = minY + (int)(Math.random() * (maxY - minY + 1));
    time = 0.0f;
    gameOver = false;
    sprite.setPosition(x, y);
}

If you do sprite.setPosition(x, y) , then modifying x and y is not enough to change the sprite's position. You should improve your update(float delta) method in this way, to ensure the sprite's position is correctly updated.

public void update(float delta){
    time += delta;
    if(time >= 3.0f)
        gameOver = true;
    sprite.setPosition(x, y);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM