计时器崩溃我的LibGdx应用程序?

时间:2017-01-19 12:23:05

标签: java android libgdx

我正在Libgdx制作一个2D游戏,当我尝试让玩家每隔X秒拍摄一次就会崩溃,只有当我在计时器内调用addBullet()时才会发生这种情况。

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

以下是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;
    }
}

1 个答案:

答案 0 :(得分:-1)

如果没有看到完整异常,我的猜测是图片加载可能有问题。

PS。为什么要加括号?

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

一组括号就足够了:

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