Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Scalafx动画计时器导致递归:可以避免吗?_Java_Scala_Javafx_Scalafx - Fatal编程技术网

Java Scalafx动画计时器导致递归:可以避免吗?

Java Scalafx动画计时器导致递归:可以避免吗?,java,scala,javafx,scalafx,Java,Scala,Javafx,Scalafx,我正在尝试做一个游戏,它使用类AnimationTimer来处理它。我的代码摘要如下所示: 主类 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 ins

我正在尝试做一个游戏,它使用类AnimationTimer来处理它。我的代码摘要如下所示:

主类

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()
}
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()
}
重启控制器

@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

}
有时它将+=2甚至更多


有没有什么解决方案可以使它不递归,就像我退出游戏时一样,旧的showGame()方法将在我开始新的showGame()方法之前完全结束???

我解决了这个问题,没有回调相同的函数,而是重新初始化了整个程序,如下所示:

射击游戏

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

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

        toBeRemove
    }
}
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){
                                    restartGame
                                }
                            })
                            gameOver.start
                        }
                    })
                    timer.start
                }
            }
        }
    }

    def restartGame(){
        //show restart layout
    }

    showMenu()
}
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()
}
object游戏扩展JFXApp{
def显示菜单{
//显示.fxml布局和控制器的代码将处理控制器
}
def显示指令{
//显示.fxml指令的代码
}
def showGame():单位={
this.root.center={
新锚烷(){
children=新组(){
val timer:AnimationTimer=AnimationTimer(t=>{
//游戏代码
如果(playerDie){
计时器。停止
val gameOver:AnimationTimer=AnimationTimer(t=>{
如果(exitPressed){
展示菜单
}否则如果(重新启动按){

对于(i尝试不回调相同的函数,而是重新初始化整个程序,如下所示:

射击游戏

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

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

        toBeRemove
    }
}
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){
                                    restartGame
                                }
                            })
                            gameOver.start
                        }
                    })
                    timer.start
                }
            }
        }
    }

    def restartGame(){
        //show restart layout
    }

    showMenu()
}
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()
}
对象游戏扩展了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()
}
def显示菜单{
//显示.fxml布局和控制器的代码将处理控制器
}
def显示指令{
//显示.fxml指令的代码
}
def showGame():单位={
this.root.center={
新锚烷(){
children=新组(){
val timer:AnimationTimer=AnimationTimer(t=>{
//游戏代码
如果(playerDie){
计时器。停止
val gameOver:AnimationTimer=AnimationTimer(t=>{
如果(exitPressed){
展示菜单
}否则如果(重新启动按){
为了