2013-06-20 62 views
-1

在我的小遊戲中,我試圖通過碰撞識別並移除一個實體,然後產生一個新的實體。我無法更改兩個數組列表「removeList」和「entities」,因爲碰撞代碼看起來像是兩個單獨的列表。我有錯誤測試它,看看實體是否碰撞並通知計算機,並且工作正常。爲什麼我不能從我的實體類中更改ArrayList?

這是我的目標衝突代碼:

public void collidedWith(Entity other) { 
    game = new Game(); 
    if (used) { 
    return; 
    } 
    if (other instanceof PlayerEntity) { 
    game.removeEntity(this); 
    game.spawnTarget();//This does work as I call it earlier in the code 

    used = true; 
    } 
} 

這裏是遊戲圈的碰撞代碼帶來的目標衝突代碼了:

for (int p = 0; p < entities.size(); p++) { 
     for (int s = p + 1; s < entities.size(); s++) { 
     Entity me = entities.get(p); 
     Entity him = entities.get(s); 

     if (me.collidesWith(him)) { 
     me.collidedWith(him); 
     him.collidedWith(me); 
     } 
     } 
    } 

下面是刪除實體代碼:

public void removeEntity(Entity entity) { 
    removeList.add(entity); 
    entities.removeAll(removeList); 
    removeList.clear(); 
    } 

只是說明:我知道我可以刪除實體而不需要removeList,但是我希望將主代碼實現到最初啓動的地方gameLoop中。我將它移至removeEntity方法來進行錯誤測試。

我剛剛開始使用實體和LWJGL,所以任何提示都會很棒。

+0

這將是很好知道爲什麼有人向下投這個。 – Capitals

回答

0

這是解決,因爲我需要使類構造函數鏈接迴游戲類。

protected TargetEntity(Game game, int x, int y) { 
     super(x, y, 10, 10); 
     this.game = game; 
    } 

這是不是...

game = new Game(); 
相關問題