0

I am trying to make a game which uses the class AnimationTimer to deal with it. The Summary of my code looks something like this:

Main Class

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

}

I have some variable such as game level that are located at some class in the companion object, so I want to avoid recursive because if it goes recursive it will cause inconsistent outcome for the level.

When the player dead, if the user quit, the user will shown the Menu, and if the player press start game again, it does not show any inconsistency for those variable in the companion object.

However, if the player press restart, it will go into recursive, which mean the method calling another method and hence the old method did not end and say if I do something like this

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

Sometimes it will += 2 or even more

Are there any solution to it that making it not recursive, which behave exactly like when I exit game and the old showGame() method will completely end before i start a new one??

1 Answers1

0

I had solved the problem by not calling back the same function, instead I reinitialize the whole program which as seen below :

ShootingGame

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

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

        toBeRemove
    }
}

Main Class

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()
}