2016-08-23 15 views
-1

我正在開發一個JavaME遊戲,我需要計算我的遊戲中碰撞的次數。 我使用collidesWith()方法,和我做這樣的事情:如何計算JavaME中collidesWith()的衝突次數?

private void checkCollision() 
{ 
    if (spBoy.collidesWith(spBall, true)) { 
      this.collides++; 
      if (this.collides == 3) { 
       //here I will show a Game Over image. 
      }     
    }   
} 

正如你所看到的,如果衝突的數量爲3,遊戲就結束了,但我不能算碰撞次數,因爲當我增加this.collides時,我自動在一次中有3次碰撞。

回答

0

我假設你在主循環內調用checkCollision()。這意味着它被稱爲每秒30-60次。 如果這兩個精靈在第二秒內根本不動,就會有30-60次碰撞 - 因爲在每個週期中都是如此。 你想要做的是添加一個定時器,你的spBoy雪碧不會受到傷害。

int safeTimer = 0; 
int timeSinceLastLoop; // Add this calculation to your loop 

private void checkCollision() { 
safeTimer-= timeSinceLastLoop; 
if (spBoy.collidesWith(spBall, true) && safeTimer<=0) { 
    this.collides++; 
    safeTimer=3000; // Wait 3 seconds till vulnerability 
    if (this.collides == 3) { 
    //here I will show a Game Over image. 
    }     
}   
}