在范围内无法访问类型为AnimationTimer的封闭实例

时间:2017-01-16 19:05:10

标签: javafx

我正在使用JavaFX创建自己的Pong游戏版本。在显示最终得分后,单击给定按钮返回主菜单,我试图开始备份我的AnimationTimer或循环。但是,我收到此错误,“没有在范围内访问类型AnimationTimer的封闭实例”。如何在按钮的.setOnAction中重新启动AnimationTimer?

以下是我的AnimationTimer的代码

new AnimationTimer() {
        public void handle(long now) {
            if(playing) {
                x += dx;
                y += dy;

                if ( x < 0 || x+ball.getWidth() > scene.getWidth() )
                    dx = -dx;
                if (y < 0 )
                    dy = -dy;

                if(y > scene.getHeight() && lives > 0) {
                    x = 400;
                    y = 400;
                    dx = 5;
                    dy = -5;
                    lives-=5;
                    life.setText("Lives: " + lives);
                }

                if(left && pX > 5) {
                    pX -= speed;
                } else if(right && pX+paddle.getWidth() < scene.getWidth() - 5) {
                    pX += speed;
                }


                if (x > pX-40 && x < pX + paddle.getWidth() && y == pY-paddle.getHeight()-15) {
                    dy = -dy;
                    score+=1;
                    scores.setText("Score: " + score);
                }



                life.relocate(10, 10);
                ball.relocate(x, y);
                paddle.relocate(pX, pY);
            }

            if(lives == 0) {
                playing = false;
                gameover = true;
                this.stop();
                root.getChildren().removeAll(paddle, ball, scores, life);
                finalScore.setText("" + score);
                root.getChildren().add(finalScore);
                root.getChildren().add(returnToMain);
                returnToMain.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent e) {
                        playing = false;
                        gameover = false;
                        root.getChildren().addAll(start, pong);
                        root.getChildren().removeAll(finalScore, returnToMain);

                        AnimationTimer.this.start();
                    }
                });
            }       
        }
    }.start();

0 个答案:

没有答案