我正在创建一个纸牌游戏并将 slick2d 用于图形。当我在 public void init 中调用我的方法从一副牌中抽一张牌时,它会运行两次。下面的方法从卡片组构造函数中抽取一张卡片并返回索引号。
public Card PlayerCardDraw()
{
Card index = cards.remove(1);
System.out.println("Cards left: " + cards.size());
return index;
}
甲板构造函数:
Deck()
{
cards = new ArrayList<Card>();
for (int a = 0; a < 52; a++)
{
cards.add(new Card(a));
}
}
public class Play extends BasicGameState
{
Deck deck = new Deck ();
Image PlayScreen;
Image PauseButton;
Image FaceDown1, FaceDown2;
Image card[] = new Image [52];
public String mouse = "no input";
Card C;
public Play (int state)
{
}
public void init (GameContainer gc, StateBasedGame sbg) throws SlickException
{
PlayScreen = new Image ("res/Play/PlayScreen.png");
PauseButton = new Image ("res/Play/PauseButton.png");
FaceDown1 = new Image("res/CardBacks/redback1.png");
FaceDown2 = new Image("res/CardBacks/redback2.png");
String fileLocation = new String ();
for (int i = 0 ; i < 52 ; i++)
{
fileLocation = "res/cards/" + (i+1) + ".png";
card [i] = new Image (fileLocation);
}
//should only run once, right?
C = deck.PlayerCardDraw();
}
我跑时得到的输出是 Cards left: 51 Cards left: 50
这意味着 PlayerCardDraw() 被调用了两次。我在一个简单的系统输出语句中添加了“嗨”,它也运行了两次。
有人知道为什么会这样以及如何解决吗?顺便说一句,这东西在程序进入播放状态之前就已经运行了。它在标题状态下显示左卡和 hi 卡,其中不包括任何这些。