我在這裏有點問題,真的,我不明白爲什麼。我寫了大量代碼,它們在參數中使用字段變量,然後更改它們。下面是方法:方法不改變變量
public void hit(Array<Card> cards, int score, float spacing) {
// Take the last card from the deck and store it in a temp card variable
nextCard = deck.pop();
// If the card value is equal to 11, it must be an ace, if it puts the
// player over 21 it makes the ace have a value of 1
if ((nextCard.getCardRealValue() == 11)
&& nextCard.getCardRealValue() + score > 21) {
nextCard.setCardRealValue(1);
}
// Add the card to the array passed in the parameter
cards.add(nextCard);
// Check the last card in the array (the one just added) grab it's value, add it to total
score += cards.peek().getCardRealValue();
// Shrink the main deck, not sure if neccessary but not point in leaving empty memory
deck.shrink();
// Move the sprites 20 pixels
spacing -= 20;
// tbh this bit never gets called, stupid useless code lol
if (score > 21) {
if (cards.peek().getCardRealValue() == 11) {
cards.peek().setCardRealValue(1);
score -= 10;
}
}
// if the first card has not been checked and score is over 21, if it is an ace change it to a value of 1
if (!aceOne && score > 21 && cards.get(0).getCardRealValue() == 11) {
cards.get(0).setCardRealValue(1);
score -= 10;
aceOne = true;
// Same as above, just if second card is ace
} else if (!aceTwo && score > 21
&& cards.get(1).getCardRealValue() == 11) {
cards.get(1).setCardRealValue(1);
score -= 10;
aceTwo = true;
}
}
這基本上是當玩家決定「打」被調用的方法,它需要保存玩家的牌陣,球員得分,然後這個東西我有叫間距,這基本上將卡精靈-20左移停止,然後拖到屏幕的右側,看起來很蠢。
現在,當我嘗試調用這個方法:
hit(playerCards, playerScore, playerSpacing);
爲什麼playerScore和playerSpacing沒有更新?牌被添加得很好,因爲他們畫出了受人尊敬的雪碧,我錯過了什麼?
就我所知「Java總是按值傳遞」並不是一個真實的說法。請參閱http://stackoverflow.com/questions/40480/is-java-pass-by-reference –