2017-01-19 7 views
0

Ich mache ein 2D-Spiel in Libgdx und wenn ich versuche, einen Spieler alle X Sekunden zu schießen, stürzt es ab und das passiert nur, wenn ich addBullet() in einem Timer anrufe.Timer stürzt meine LibGdx App ab?

Hier Code für Controller.class ist:

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

Hier ist der Code für die bullet.class ist:

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

Bitte geben Sie die Fehlermeldung (Exception). – munyul

+0

bei java.util.Timer $ TimerImpl.run (Timer.java:284) – Nikola

+0

Wenn das Spiel abstürzt, wird irgendwo in den Protokollen eine Ausnahme angezeigt - finden Sie, dass es hilft. – munyul

Antwort

-1

Ohne die voll Ausnahme zu sehen, meine Vermutung ist, dass es vielleicht etwas falsch mit dem Laden des Bildes sein.

ps. Warum die zusätzlichen Klammern?

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

Ein Satz von Klammern ist genug:

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