我正在尝试做一个记忆游戏并且当我想要为玩家显示两张牌时出现轻微问题。
当我运行我的记忆游戏时发生的事情是第一张牌正确显示但当我点击第二张牌时它会一直面朝下,除非它是一对。
if (model.getCard(tile.getNr()).getCardState() == CardState.DOWN) {
model.getCard(tile.getNr()).setCardState(CardState.UP);
tile.setImg(model.getCard(tile.getNr()).getValue()); //sets the correct image onto the card
click--;
//test if equal
if (click == 0) {
for (Cards card1 : cards) {
if (card1.getCardState() == CardState.UP) {
if (tile.getNr() != cards.indexOf(card1)) {
if (model.getCard(tile.getNr()).getValue() == card1.getValue()) {
//if both cards are equal, set them both to paired
model.getCard(tile.getNr()).setCardState(CardState.PAIRED);
model.getCard(cards.indexOf(card1)).setCardState(CardState.PAIRED);
click = 2;
}
}
}
}
try {
Thread.sleep(500); //1000 milliseconds is one second.
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
for (Cards card1 : cards) {
if (card1.getCardState() != CardState.PAIRED && card1.getCardState() == CardState.UP) {
model.getCard(cards.indexOf(card1)).setCardState(CardState.DOWN);
tiles.get(cards.indexOf(card1)).setCardDown();
}
}
click = 2;
}
}
我有延迟,所以玩家可以在转牌前看到两张牌。
感谢您的帮助!
答案 0 :(得分:1)
使用Thread.sleep(500);
阻止应用程序线程阻止UI更新。您需要以允许应用程序线程继续的方式等待,例如使用PauseTransition
。
if (click == 0) {
... turn card face up ...
PauseTransition transition = new PauseTransition(Duration.millis(500));
transition.setOnFinished(evt -> {
... turn cards face down ...
});
transition.play();
}