2016-10-22 1 views
0

저는 메모리 게임을하려고하는데 플레이어에 대해 두 개의 카드를 위로 돌리고 싶을 때 약간의 문제가 있습니다.메모리에있는 두 번째 카드가 위로 향하게하지 마십시오

내 기억 게임을 실행할 때 일어나는 일은 첫 번째 카드가 올바르게 표시되지만 두 번째 카드를 클릭하면 얼굴이 아래로 향하게됩니다.

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; 
       } 
      } 

플레이어가 뒤집기 전에 두 카드를 모두 볼 수 있도록 지연이 있습니다.

도와 주셔서 감사합니다.

답변

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(); 
} 
+0

감사합니다. 이게 내 문제를 해결했습니다! – ZeppRock

관련 문제