用JavaFX8制作一个游戏。如何从初始页面切换到剪切场景到关卡?
这是我第一次使用JavaFX玩游戏,所以我承认我做了一些很糟糕的设计决定 无论如何,我想从一个初始页面(splash类)转换到一个cutscene(cutscene类),然后再转换到一个可玩的级别(PlayableLevel类)。游戏是从我的主课开始的,过渡应该通过键盘输入完成(输入按钮) 开始游戏的主要方法如下所示:用JavaFX8制作一个游戏。如何从初始页面切换到剪切场景到关卡?,java,javafx,Java,Javafx,这是我第一次使用JavaFX玩游戏,所以我承认我做了一些很糟糕的设计决定 无论如何,我想从一个初始页面(splash类)转换到一个cutscene(cutscene类),然后再转换到一个可玩的级别(PlayableLevel类)。游戏是从我的主课开始的,过渡应该通过键盘输入完成(输入按钮) 开始游戏的主要方法如下所示: public void start (Stage s) { // create your own game here Splash splashP
public void start (Stage s) {
// create your own game here
Splash splashPage = new Splash();
Cutscene cs = new Cutscene();
PlayableLevel play = new PlayableLevel();
// attach game to the stage and display it
Scene scene0 = splashPage.init(SIZE, SIZE);
Scene scene1 = cs.init(SIZE, SIZE, 0);
Scene scene2 = play.init(SIZE, SIZE, 0);
s.setScene(scene0);
s.show();
// sets the game's loop
KeyFrame frame = new KeyFrame(Duration.millis(MILLISECOND_DELAY),
e -> myGame.step(SECOND_DELAY));
Timeline animation = new Timeline();
animation.setCycleCount(Timeline.INDEFINITE);
animation.getKeyFrames().add(frame);
animation.play();
}
public class Splash {
private Runnable nextSceneHandler ;
public void setNextSceneHandler(Runnable handler) {
nextSceneHandler = handler ;
}
public Scene init(double width, double height) {
Scene scene = new Scene();
// Just an example handler, you could do the same for
// button events, menus, etc., or even just handlers for the
// end of an animation
scene.setOnKeyPressed(e -> {
if (nextSceneHandler != null) {
if (e.getCode() == ...) {
nextSceneHandler.run();
}
}
}
// existing code...
}
// existing code ...
}
我的问题是,我应该怎么做才能使Splash类能够与主类通信,这样一旦记录了击键事件,stage就可以设置一个新场景?我目前正在阅读有关EventHandler的文章,但目前还不确定具体的实现
编辑:我的一个想法是制作一个场景链接列表,然后一旦发生某个事件(击键),我就会将场景设置为列表中的下一个场景 您可以这样做:
public void start (Stage s) {
// create your own game here
Splash splashPage = new Splash();
Cutscene cs = new Cutscene();
PlayableLevel play = new PlayableLevel();
// attach game to the stage and display it
Scene scene0 = splashPage.init(SIZE, SIZE);
Scene scene1 = cs.init(SIZE, SIZE, 0);
Scene scene2 = play.init(SIZE, SIZE, 0);
s.setScene(scene0);
s.show();
// sets the game's loop
KeyFrame frame = new KeyFrame(Duration.millis(MILLISECOND_DELAY),
e -> myGame.step(SECOND_DELAY));
Timeline animation = new Timeline();
animation.setCycleCount(Timeline.INDEFINITE);
animation.getKeyFrames().add(frame);
animation.play();
}
public class Splash {
private Runnable nextSceneHandler ;
public void setNextSceneHandler(Runnable handler) {
nextSceneHandler = handler ;
}
public Scene init(double width, double height) {
Scene scene = new Scene();
// Just an example handler, you could do the same for
// button events, menus, etc., or even just handlers for the
// end of an animation
scene.setOnKeyPressed(e -> {
if (nextSceneHandler != null) {
if (e.getCode() == ...) {
nextSceneHandler.run();
}
}
}
// existing code...
}
// existing code ...
}
对于CutScene
也是如此
然后
你的链表想法也应该行得通。您需要一种机制来将链表实例(或者其中的迭代器)传递给每个场景生成类;他们的事件处理程序将执行如下代码
scene.getWindow().setScene(sceneIterator.next());
我比较喜欢在对象上设置runnable,因为它感觉有点灵活。不过,这只是一个风格问题