Libgdx类不绘制纹理

时间:2019-03-13 12:21:34

标签: java android libgdx textures draw

当我触摸手机的屏幕时,我试图从“ germans.java”类的“ MainGameScreen.java”类中绘制游戏角色。
不幸的是,我的程序没有绘制图像,也没有绘制图像它会给我警告或错误。

MainGameScreen.java:

import com.daenni.trenchwarfare.mygdx.enteties.germans;

public class MainGameScreen implements Screen, InputProcessor {

Trench_Warfare game;


public SpriteBatch batch;

//Enemies
ArrayList<germans> german;

public MainGameScreen (Trench_Warfare game) {
    this.game = game;
    batch = new SpriteBatch();

    //Enemies
    //Initialise Array
    german = new ArrayList<germans>();
}

@Override
public void render(float delta) {

    //Colours
    Gdx.gl.glClearColor(116/255f,102/255f,91/255f,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    //Create Germans

    if (Gdx.input.justTouched()){
        german.add(new germans(300));
        german.add(new germans(400));
    }

    //Update Germans
    for (germans german : german) {
        german.update(delta);
    }

    game.batch.begin();


    //Render Germans
    for (germans germans : german) {
        germans.render(game.batch);
    }

    //Background
    game.batch.draw(background,0,0);
    game.batch.draw(background_links,-background_links.getWidth(),0);
    game.batch.draw(background_rechts,background.getWidth(),0);

    game.batch.end();
}

这是我用来在“ MainGameScreen.java”文件中呈现它的所有代码。

这是我的课程:

public class germans {
    //Set speed
    public static final int speed = 25;

    //Constant
    public static final int default_x = 300;

    //Every german uses the same Texture
    private static Texture texture;

    //Position
    float x, y;

    public boolean remove = false;

    //Create german
    public germans(float y) {
        this.x = default_x;
        this.y = y;
        y = 200;

        if (texture == null) { //When texture is never loaded
            //Set Texture
            texture = new Texture("de_s1_default.png");
        }
    }

    public void update (float deltaTime){
            x += speed * deltaTime;
    }

    public void render (SpriteBatch batch) {
        batch.draw(texture,x,y);
    }

}

1 个答案:

答案 0 :(得分:1)

尽管我不喜欢libgdx的工作原理,但我很确定首先要绘制“ germans”,然后背景不是您想要的。

尝试将其交换:

//Background
game.batch.draw(background,0,0);
game.batch.draw(background_links,-background_links.getWidth(),0);
game.batch.draw(background_rechts,background.getWidth(),0);

    //Render Germans
for (germans germans : german) {
    germans.render(game.batch);
}