2017-07-07 21 views
0

我在做一個大學項目,我遇到了我的EntiyManager更新方法問題。由於某種原因,這個更新系統讓我的子彈按照我的實體數組大小移動得更快,如果這個數組大小大於1,子彈將移動得更快,如果不是子彈將正常移動。根據實體數組大小,更新方法表現不同。

我想讓我所有的子彈正常移動。

注:b.getVel()和VEL總是2.

EntityManager的方法:

public void update() 
{ 
    if(!handler.getPaused()) 
    { 
     for(int i = 0; i < creatures.size(); i++) 
     { 
      //entities[i]  at 
      Creatures c = creatures.get(i); 
      c.update(); 

      if(!bullets.isEmpty()) 
      { 
       for (int j = 0; j < bullets.size(); j++) 
       { 
        Bullet b = bullets.get(j); 
        //Hit detection; 
        if(c.getCollisionBounds(b.getVel(), 0).intersects(b.getCollisionBounds(0, 0))) 
        { 
         c.HP -= 5; 
         removeBullet(b); 
        } 
        System.out.println(b.getVel()); 
        b.update(); 
       } 
      } 
      else System.out.println("empty"); 
     } 

    } 

} 



public void draw(Graphics g) 
{ 

    for(Entity se : staticEntities) 
    { 
     se.draw(g); 
    } 

    for(Bullet b : bullets) 
    { 
     b.draw(g); 
    } 

    for(Creatures c : creatures) 
    { 
     c.draw(g); 
    } 
} 

子彈的方法:

@Override 
public void update() 
{ 
    if(vel > 0) 
    { 
     txRight = (int) (x + vel + bounds.x + bounds.width)/Tile.TILESIZE; 
    } 
    else 
    { 
     txLeft = (int) (x + vel + bounds.x)/Tile.TILESIZE; 
    } 

    if(collisionWithTile(txRight, (int) (y + bounds.y)/Tile.TILESIZE) || 
     collisionWithTile(txRight, (int) (y + bounds.y + bounds.height)/Tile.TILESIZE) || 
     collisionWithTile(txLeft, (int) (y + bounds.y)/Tile.TILESIZE) || 
     collisionWithTile(txLeft, (int) (y + bounds.y + bounds.height)/Tile.TILESIZE)) 
    { 
     handler.getLevel().getEntityManager().removeBullet(this); 
    } 

    x += vel;   
} 

@Override 
public void draw(Graphics g) 
{ 
    g.setColor(Color.yellow); 
    g.fillRect((int) x, (int) y, width, height);  
} 

回答

0

我現在知道爲什麼會這樣。它正在更新兩次子彈。