Search code examples
javaandroidlibgdx

Timer is crashing my LibGdx app?


I am making a 2D game in Libgdx and when I try to make a player shoot every X seconds it crashes and that happens only if I call addBullet() inside a timer.

Here is code for Controller.class:

public class Controller  {

    private LinkedList<Bullet> b = new LinkedList<Bullet>();
    private Bullet tempBullet;

    private LinkedList<Zombie> z = new LinkedList<Zombie>();
    private Zombie tempZombie;

    private API api;
    private Player p;
    private World w;

    public void create() {
        api = new API();
        w = new World();
        p = new Player(100, 40);
        Timer t = new Timer( );
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                // Crashes when this is executed
                addBullet(new Bullet(p.getX() + p.getTex().getWidth(), p.getY() + p.getTex().getHeight()));
            }
        }, 500,2500);
    }

    public void tick() {
        p.tick();
        for(int i = 0; i < b.size(); i++){
            tempBullet = b.get(i);
            tempBullet.tick();
        }
        for(int i = 0; i < z.size(); i++){
            tempZombie = z.get(i);
            tempZombie.tick();
            for(int j = 0; j < b.size(); j++){
                tempBullet = b.get(j);
                if(api.getCollisionBox(tempZombie.getX(), tempZombie.getY(), tempZombie.getTex().getWidth(), tempZombie.getTex().getHeight(), tempBullet.getX(), tempBullet.getY(), 10, 10)){
                    tempZombie.damage();
                    b.remove(tempBullet);
                    if(tempZombie.getHealth() <= 0){
                        z.remove(tempZombie);
                    }
                }
            }
        }
    }

    public void render() {
        w.render(); // Mora biti na pocetku ove funkcije
        p.render();
        for(int i = 0; i < b.size(); i++){
            tempBullet = b.get(i);
            tempBullet.render();
        }
        for(int i = 0; i < z.size(); i++){
            tempZombie = z.get(i);
            tempZombie.render();
        }
    }

    public void dispose() {
        w.dispose();
        p.dispose();
        for(int i = 0; i < b.size(); i++){
            tempBullet = b.get(i);
            tempBullet.dispose();
        }
        for(int i = 0; i < z.size(); i++){
            tempZombie = z.get(i);
            tempZombie.dispose();
        }
    }

    public void keyInput() {
        //if(Gdx.input.isButtonPressed(Input.Keys.W))
    }

    public void mouseInput() {
        //if(Gdx.input.isButtonPressed(Input.Buttons.LEFT))
        if(Gdx.input.isTouched())
            addBullet(new Bullet(p.getX() + p.getTex().getWidth(), p.getY() + p.getTex().getHeight()));
    }

    public void addBullet(Bullet block){
        b.add(block);
    }

    public void removeBullet(Bullet block){
        b.remove(block);
    }

    public void addZombie(Zombie block){
        z.add(block);
    }

    public void removeZombie(Zombie block){
        z.remove(block);
    }
}

Here is the code for the bullet.class:

public class Bullet {

    private Texture tex;
    private SpriteBatch batch;

    private int x;
    private int y;

    public Bullet(int x, int y){
        this.x = x;
        this.y = y;
        batch = new SpriteBatch();
        tex = new Texture(Gdx.files.internal(("bullet.png")));
    }

    public void tick(){
        x += 10;
    }

    public void render(){
        batch.begin();
        batch.draw(tex, x, y, 100, 100);
        batch.end();
    }

    public void dispose() {
        tex.dispose();
        batch.dispose();
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

Solution

  • Without seeing the full exception, my guess is that there might be something wrong with the loading of the image.

    ps. Why the extra brackets?

    tex = new Texture(Gdx.files.internal(("bullet.png")));
    

    One set of brackets is enough:

    tex = new Texture(Gdx.files.internal("bullet.png"));