2015-04-28 24 views
0

在我的遊戲我有一個Bullet類,它是負責制定新的子彈每次槍射擊的時間。在創建後,子彈被添加到Bullets類中,該類負責跟蹤所述子彈以及其餘子彈。我所遇到的一個奇怪的現象:
殺死一個敵人,然後再拍攝一次後,新的子彈有以下特點:
Box2D的再造一個身體

  1. 子彈是同一個(在相同的代碼編號)作爲殺死敵人的子彈 。 (IE如果ID是: [email protected],那麼這將是 完全相同的ID。)
  2. 子彈會出現卡住的地方,它的精靈不動,而是根據遊戲將有一個速度,但該位置保持不變。唯一可見的運動是當你啓用Box2DDebugRenderer時,你可以看到身體向下移動,直到擊中地面,然後他「傳送」回來並緩慢地向下回落。
  3. 卡住的子彈數量等於死亡的敵人數量。

這是子彈類:

public class Bullet { 

private Body bullet; 

public Bullet(final float force, final int bulletDmg, final Weapon weapon, 
     final World world) { 
    System.out.println("Position " + weapon.getPosition() + ", Angle: " 
      + weapon.getAngle()); 
    final BodyDef bulletDef = new BodyDef(); 
    bulletDef.type = BodyDef.BodyType.DynamicBody; 
    bulletDef.angle = weapon.getAngle(); 
    bulletDef.position.set(
      weapon.getPosition().x 
        + (float) (2.5 * MathUtils.cos(weapon.getAngle())), 
      weapon.getPosition().y 
        + (float) (2.5 * MathUtils.sin(weapon.getAngle()))); 
    bulletDef.angle = weapon.getAngle(); 
    PolygonShape bulletShape_1 = new PolygonShape(); 
    bulletShape_1.setAsBox(0.34375f, 0.34375f); 
    CircleShape bulletShape_2 = new CircleShape(); 
    bulletShape_2.setPosition(new Vector2(0.34375f, 0)); 
    bulletShape_2.setRadius(0.34375f); 
    final FixtureDef bulletFixture_1 = new FixtureDef(); 
    bulletFixture_1.density = 1f; 
    bulletFixture_1.shape = bulletShape_1; 
    bulletFixture_1.friction = 0.25f; 
    bulletFixture_1.restitution = 0.75f; 
    final FixtureDef bulletFixture_2 = new FixtureDef(); 
    bulletFixture_2.density = 1; 
    bulletFixture_2.shape = bulletShape_2; 
    bulletFixture_2.friction = 0.25f; 
    bulletFixture_2.restitution = 0.75f; 
    final Timer creationTimer = new Timer(); 
    creationTimer.scheduleTask(new Task() { 

     @Override 
     public void run() { 
      if (!world.isLocked()) { 
       System.out.println(bullet); 
       bullet = world.createBody(bulletDef); 
       bullet.createFixture(bulletFixture_1); 
       bullet.createFixture(bulletFixture_2); 
       System.out.println(bullet); 
       bullet.applyForceToCenter(
         force * MathUtils.cos(weapon.getAngle()), force 
           * MathUtils.sin(weapon.getAngle()), true); 
       Sprite sprite = new Sprite(new Texture(
         "sprites\\Weapon\\bullet_standard.png")); 
       sprite.setSize(1.03125f, 0.6875f); 
       sprite.setOrigin((float) (sprite.getWidth()/2 - 0.12f), 
         (float) (sprite.getHeight()/2)); 
       bullet.setUserData(sprite); 
       Bullets bullets = Bullets.getInstance(world); 
       bullets.addBullet(bullet); 
       bullets.setDmg(bulletDmg); 
       System.out.println("Create bullet number: " + bullet); 
       creationTimer.stop(); 
      } 
     } 

    }, 0, 1); 
    creationTimer.start(); 
} 

}

我一直在面臨這個相當長的一段時間,想不通的問題,我很樂意與一些這方面的援助。提前致謝!


更新1:
我不重用任何創建的子彈。
這是處理衝突與敵人代碼:

public void onCollision(final Body collidedBody, final String bodyHit, 
     final int index) { 
    assert instance != null; 
    final Timer timer = new Timer(); 
    timer.scheduleTask(new Task() { 
     @Override 
     public void run() { 
      if (!world.isLocked()) { 
       Circles circles = Circles.getInstance(); 
       if (bodyHit.equalsIgnoreCase("ground")) { 
        if (bulletGroundCollision.get(index) == 5) { 
         if (bullets.get(index) != null) { 
          world.destroyBody(bullets.get(index)); 
          bullets.removeIndex(index); 
          bulletGroundCollision.removeIndex(index); 
         } 
        } else 
         bulletGroundCollision.set(index, 
           (bulletGroundCollision.get(index) + 1)); 
       } else if (bodyHit.equalsIgnoreCase("enemy")) { 
        Circle enemy = circles 
          .findAccordingToCode(collidedBody); 
        enemy.damaged(bulletDmg); 
        System.out.println("Hit at: " 
          + bullets.get(index).getPosition()); 
        if (bullets.get(index) != null) { 
         world.destroyBody(bullets.get(index)); 
         bullets.removeIndex(index); 
         bulletGroundCollision.removeIndex(index); 
        } 
       } else if (bodyHit.equalsIgnoreCase("player")) { 
        if (bullets.get(index) != null) { 
         world.destroyBody(bullets.get(index)); 
         bullets.removeIndex(index); 
         bulletGroundCollision.removeIndex(index); 
        } 
        Square square = Square.getInstance(world); 
        square.damaged(bulletDmg); 
       } 
       timer.stop(); 
      } 
     } 

    }, 0, 1); 
    timer.start(); 
} 

的子彈創建的代碼已經被公佈爲bullet類。
bullets - 是要被子彈機構的Array
bulletGroundCollision - 是整數的Array以跟蹤在位置i(即指數)子彈多少次,擊中地面。

更新2
我注意到,子彈被卡住後,與子彈的碰撞不根據身體情況發生,取而代之的則是隻有當有與碰撞觸發精靈的碰撞。

+0

您是否使用'Bullets'類重用'Bullets'?此外,請顯示處理「Bullet」何時擊中敵人以及何時創建「Bullet」的代碼。 – asherbar

+0

我編輯了這篇文章,'bullet'創建時的代碼是'bullet'類。 – RedDeadRedemption

+0

@RedDeadRedemption,您究竟如何檢測碰撞?你使用Box2D嗎?我問你是因爲你提到了一個「精靈」和一個「精靈」之間的碰撞。 Box2D當然不知道這一點。 –

回答

0

您的代碼有點難以閱讀。然而,你需要確保在你的敵人死亡後,你需要再次使用你的子彈類中的更新方法來更新你的子彈類。

boolean enemyDead; 

if(damage<=0) 
{ 
bulletClass.updateBulletLocation(); 
} 

我看起來並不像它,我認爲要更新子彈類,你殺死敵人後,也正因爲如此,子彈停留在哪裏。