为了避免混乱,我为两个文件划分了工作代码。它以前起过作用,但是把所有的场景都放在一节课上是很不愉快的。
之前,当你点击雪碧,它会带你从菜单到游戏。THen I用游戏组和游戏类提取代码。
现在我可以看到,当我按下雪碧时,上面写着“点击”。这意味着改变环境是可行的。问题是,把文件分割后,第二场景(游戏场景)显示的是第一个场景的内容,而不是自己的。可能没有重新粉刷。我怎么才能修好它?谢谢
主类代码(菜单窗口):
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class EventFiltersExample extends Application {
@Override
public void start(Stage stage) {
Image playerImage = new Image("body.png");
ImageView playerImageView = new ImageView(playerImage);
playerImageView.setX(50);
playerImageView.setY(25);
Text text = new Text("Zegelardo");
text.setFont(Font.font(null, FontWeight.BOLD, 40));
text.setX(150);
text.setY(50);
Group menuGroup = new Group(playerImageView,text);
//Group gameGroup = new Group();
Scene menuScene = new Scene(menuGroup, 600, 300);
//Scene gameScene = new Scene(gameGroup, 600, 300);
stage.setTitle("Zegelardo");
stage.setScene(menuScene);
stage.show();
GameGroup gamegroup = new GameGroup();
EventHandler <MouseEvent> eventHandler = new EventHandler <MouseEvent>() {
@Override
public void handle(MouseEvent e) {
stage.setScene(gamegroup.gameScene);
System.out.println("Clicked.");
}
};
playerImageView.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
}
public static void main(String args[]){
launch(args);
}
}具有游戏窗口构造函数/方法的类代码:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.Parent;
import javafx.scene.Group ;
import javafx.scene.Parent ;
import javafx.scene.shape.Line ;
import javafx.stage.Stage;
public class GameGroup {
public Group gameGroup;
public Scene gameScene;
public GameGroup() {
Image playerImage = new Image("body.png");
ImageView playerImageView = new ImageView(playerImage);
playerImageView.setX(50);
playerImageView.setY(25);
Group gameGroup = new Group(playerImageView);
Scene gameScene = new Scene(gameGroup, 600, 300);
}
public Parent getView() {
return gameGroup ;
}
}发布于 2017-08-13 13:18:20
在GameGroup构造函数中,新建两个局部变量,而不是初始化字段。
将GameGroup.java改为
Group gameGroup = new Group(playerImageView);
Scene gameScene = new Scene(gameGroup, 600, 300);至
gameGroup = new Group(playerImageView);
gameScene = new Scene(gameGroup, 600, 300);https://stackoverflow.com/questions/45660380
复制相似问题