2017-07-01 37 views
1

我想製作一個使用類AnimationTimer來處理它的遊戲。我的代碼的摘要看起來是這樣的:導致遞歸的Scalafx動畫計時器:可以避免這種情況嗎?

主類

object Game extends JFXApp{ 

    def showMenu{ 
     //code that show the .fxml layout and controller will handle the controller 
    } 

    def showInstruction{ 
     //code that show the .fxml instruction 
    } 

    def showGame():Unit = { 
     this.roots.center = { 
      new AnchorPane(){ 
       children = new Group(){ 

        val timer:AnimationTimer = AnimationTimer(t=> { 
         //game code 

         if(playerDie){ 
          timer.stop 
          val gameOver:AnimationTimer = AnimationTimer(t => { 
           if(exitPressed){ 
            showMenu 
           } else if (restartPressed){ 
            restartGame 
           } 
          }) 
          gameOver.start 
         } 
        }) 
        timer.start 
       } 
      } 
     } 
    } 

    def restartGame(){ 
     //show restart layout 
    } 

    showMenu() 
} 

RestartController

@sfxml 
class RestartController(private val countDownLabel:Label){ 
    var lastTimer:Double = 0 
    var countDownSec:Double = 5.999 
    val countDown:AnimationTimer = AnimationTimer(t => { 
     val delta = (t-lastTimer)/1e9 
     if(delta < 1) countDownSec -= delta 
     countDownLabel.text = countDownSec.toInt.toString 
     if(countDownSec <= 0) { 
      countDown.stop 
      Game.showGame 
     } 
     lastTimer = t 
    })//end AnimationTimer pauseTimer 
    countDown.start 

    //I think Game.showGame should be located at here but tried several ways still can't implement it 

} 

我有一些變量如位於某個類的遊戲關卡伴隨對象,所以我想避免遞歸,因爲如果遞歸它將導致關卡的結果不一致。

當玩家死了,如果用戶退出,用戶將顯示菜單,並且如果玩家再次按下開始遊戲,則它不會顯示伴侶對象中的這些變量的任何不一致。

但是,如果玩家按下重啓後,將進入遞歸的,這意味着該方法調用另一個方法,因此舊的方法並沒有結束,並說,如果我做這樣的事情

ShootingGame.level += 1 //be trigger when certain requirement meet 

有時將+ = 2甚至更多

是否有任何解決方案,使其不遞歸,其行爲完全一樣,當我退出遊戲和舊的showGame()方法將完全結束之前,我開始一個新的?

回答

1

不要回調相同的功能,而是我reini tialize整個程序,其如下所示:

射擊遊戲

class ShootingGame{ 
    def restart:ListBuffer[Circle] = { 
     var toBeRemove:ListBuffer[Circle] 

     //initialize and add those needed to be remove into toBeRemove 

     toBeRemove 
    } 
} 

主類

對象遊戲延伸JFXApp {

def showMenu{ 
     //code that show the .fxml layout and controller will handle the controller 
    } 

    def showInstruction{ 
     //code that show the .fxml instruction 
    } 

    def showGame():Unit = { 
     this.roots.center = { 
      new AnchorPane(){ 
       children = new Group(){ 

        val timer:AnimationTimer = AnimationTimer(t=> { 
         //game code 

         if(playerDie){ 
          timer.stop 
          val gameOver:AnimationTimer = AnimationTimer(t => { 
           if(exitPressed){ 
            showMenu 
           } else if (restartPressed){ 
            for(i <- game.restart) children.remove(i) 
            timer.start 
           } 
          }) 
          gameOver.start 
         } 
        }) 
        timer.start 
       } 
      } 
     } 
    } 

    showMenu() 
} 
1

我從前沒有回調到相同的功能解決了這個問題,而不是我重新初始化整個程序,如下圖所示:

ShootingGame

class ShootingGame{ 
    def restart:ListBuffer[Circle] = { 
     var toBeRemove:ListBuffer[Circle] 

     //initialize and add those needed to be remove into toBeRemove 

     toBeRemove 
    } 
} 

主類

object Game extends JFXApp{ 

    def showMenu{ 
     //code that show the .fxml layout and controller will handle the controller 
    } 

    def showInstruction{ 
     //code that show the .fxml instruction 
    } 

    def showGame():Unit = { 
     this.roots.center = { 
      new AnchorPane(){ 
       children = new Group(){ 

        val timer:AnimationTimer = AnimationTimer(t=> { 
         //game code 

         if(playerDie){ 
          timer.stop 
          val gameOver:AnimationTimer = AnimationTimer(t => { 
           if(exitPressed){ 
            showMenu 
           } else if (restartPressed){ 
            for(i <- game.restart) children.remove(i) 
            timer.start 
           } 
          }) 
          gameOver.start 
         } 
        }) 
        timer.start 
       } 
      } 
     } 
    } 

    showMenu() 
} 
相關問題