Search code examples
javaandroidlibgdxtexturesdraw

Libgdx class does not draw Texture


I am trying to draw a game character in my "MainGameScreen.java" class from the "germans.java" class when I touch the screen of my phone.
Unfortunately my program does not draw the image nor does it give me a warning or an error.

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

This is all of the code that I use to render it in the "MainGameScreen.java" file.

This is my class:

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

}

Solution

  • Although I am not keen on how libgdx exactly works I am pretty sure first drawing your "germans" and then the background is not what you want.

    Try swapping it around:

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