一些背景 - 它是一個小行星游戲
我正在嘗試檢測圓和 Polygon 物件之間的碰撞,因此我將 Polygon.contains() 與圓的坐標一起使用。但出于某種未知的原因,只有有時它實際上檢測到碰撞:當第一顆流星(多邊形)出現時,我會嘗試射擊它(子彈是圓圈)它可能會檢測到它但不確定第二個和等等 - 換句話說,檢測并不總是有效。
代碼:
public void run() {
super.run();
while (true){
decreaseArr(this.shapes[0].xpoints,1);
if (!Bullet.bulletList.isEmpty()){
for (int i = 0; i< Bullet.bulletList.size() ; i ){
Bullet currentBullet= Bullet.bulletList.get(i);
if (this.shapes[0].contains(currentBullet.x, currentBullet.y)){
System.out.println(meteorList.indexOf(this));
System.out.println("HIT!");
Bullet.bulletList.remove(currentBullet); //Delete bullet from screen
meteorList.remove(this); //Delete from screen
breakFlag=true;
System.out.println(Meteor.meteorList);
}
}
if (breakFlag)
break;
}
try {
TimeUnit.MILLISECONDS.sleep(meteorVelocity);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
(這是 Meteor 類中的 run 方法,所以每個流星都有一個 shape 陣列(現在它只有一個形狀,但稍后我會添加更多))
編輯: 一些視頻片段(抱歉沒有早點編輯,剛回家:))
編輯2:
重要提示:每次我運行游戲時,我都無法像上次一樣摧毀相同的流星——每次我得到不同的結果
uj5u.com熱心網友回復:
在對 Polygon 進行任何更改后,必須呼叫 invalidate()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396231.html
