我是一個使用Java的初學者程序員。我正在做我的第一個簡單的遊戲。我需要金幣(在我的控制器LinkedList類項目)要刪除它們都被接走後起來,更好的將是時,他們可以在一個又催生了另一個地方(我用這樣的:如何在java中刪除(並重新生成)一個項目(硬幣)。 (使用控制器類進行產卵)
Random randomGenerator = new Random();
for (int idx = 1; idx <= 1; ++idx){
int r1 = randomGenerator.nextInt(590);
this.r1=r1;
}
for (int idx = 1; idx <= 1; ++idx){
int r2 = randomGenerator.nextInt(380);
this.r2=r2;
將我的「硬幣」設置爲隨機的x和y變量)。 當我在那個隨機點產生它們時,我已經做了一切正確的事情,並且我也在我的Player類中做了一個Collision()方法。它看起來像:
private void Collision(){
for(int i = 0; i < c.size(); i++){
if(getBounds().intersects(c.get(i).getBounds())){
Game.points++;
}
}
}
該點正常工作(種),但我不能想出怎樣做才能去除硬幣後,它已被「撿到」。我當然有我的控制器類裏面的添加和removeItem方法:
public void removeItem(Coin coin){
c.remove(coin);
}
public void addItem(Coin coin){
c.add(coin);
}
如addItem是所謂的控制器的構造...... 我知道這是一個有點長,但我想,讓一切清晰。如果有人幫助我,我會非常高興,因爲如果沒有這種幫助,遊戲就毫無意義。 感謝大家誰幫助我,如果你需要一些進一步的信息說。
編輯: 這是我的代碼:
private void Collision(){
int newCount = 0;
while(iter.hasNext()){
iter.next();
for(int i=0; i<cb.size(); i++){
if(getBounds().intersects(cb.get(i).getBounds())){
Game.points++;
newCount++;
iter.remove();
}
}
}
for(int i = 0; i < newCount; i++){
controller.addItem(new Coin());
newCount--;
}
}
我有一個迭代器控制器聲明:
static Iterator<Coin> iter = c.iterator();
和getter函數,所以我可以在我的播放器類使用它:
public static Iterator<Coin> getCoinIterator(){
return iter;
}
然後在這裏它獲得從控制器到玩家類的迭代器
private Iterator<Coin> iter = Controller.getCoinIterator();
我甚至在控制器中創建了next()和hasNext()方法,但我仍然遇到一堆錯誤。
上你必須寫這個循環中,檢查碰撞,所以你的情況在玩家 –
好方法。感謝您的回覆。 c是玩家類中鏈接列表的變量以獲得硬幣邊界。我真的不知道迭代器循環,但我想這應該工作。我的問題是:在if語句中不應該爲new循環添加1以使for循環正常工作? 3.硬幣獲得隨機位置,但在控制器構造函數中不在硬幣構造函數中。我應該改變它嗎?讓我知道。我還是不明白很多事情:d –
Ragnar
好吧,我會盡力澄清我的答案 –