我试图制作一个小游戏,其中屏幕上的图块将按照一定的顺序逐个变为蓝色,然后完成后,播放器需要以相同的顺序点击图块。即使在我的" view / Follow.fxml"之后,问题仍然存在。加载后,程序将保持在" view / SplashScreen.fxml"并等待arraylist被填满。当它最终显示" view.Follow.fxml"时,瓷砖全部同时变为蓝色。
那么为什么"查看/关注.fxml"在arraylist被填满之前没有显示?
MainApp.java:
公共类MainApp扩展了Application {
private Stage primaryStage;
private BorderPane rootLayout;
public String game;
public static ArrayList<String> questions = new ArrayList<>();
public static ArrayList<String> answers = new ArrayList<>();
private int step = 0;
/**
* Constructor
*/
public MainApp() {
}
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Brainy");
initRootLayout();
showScreen("view/DefaultScreen.fxml");
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
// Give the controller access to the main app.
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows the screen inside the root layout.
*/
public void showScreen(String s){
try {
System.out.println(s);
// Load default screen.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource(s));
AnchorPane screen = (AnchorPane) loader.load();
// Set default screen into the center of root layout.
rootLayout.setCenter(screen);
switch(s){
case "view/DefaultScreen.fxml" :
// Give the controller access to the main app.
DefaultScreenController controller = loader.getController();
controller.setMainApp(this);
break;
case "view/Training.fxml" :
// Give the controller access to the main app.
System.out.println(loader.getController());
TrainingController controller1 = loader.getController();
controller1.setMainApp(this);
controller1.update(step);
break;
case "view/Performance.fxml" :
// Give the controller access to the main app.
PerformanceController controller2 = loader.getController();
controller2.setMainApp(this);
controller2.update();
break;
case "view/StudyScreen.fxml" :
// Give the controller access to the main app.
//DefaultScreenController controller = loader.getController();
//controller.setMainApp(this);
break;
case "view/GamesScreen.fxml" :
// Give the controller access to the main app.
GamesScreenController controller4 = loader.getController();
controller4.setMainApp(this);
break;
case "view/SplashScreen.fxml" :
// Give the controller access to the main app.
SplashScreenController controller5 = loader.getController();
controller5.setMainApp(this);
controller5.update(game);
String next = "";
switch(game){
case "Brevity":
next = "view/Brevity.fxml";
break;
case "Follow The Leader":
next = "view/Follow.fxml";
break;
case "Operations":
next = "view/Operations.fxml";
//next = "view/DefaultScreen.fxml";
break;
}
splash(next);
break;
case "view/Brevity.fxml" :
// Give the controller access to the main app.
BrevityController controller6 = loader.getController();
controller6.setMainApp(this);
controller6.update(0);
break;
case "view/Follow.fxml" :
// Give the controller access to the main app.
FollowController controller7 = loader.getController();
controller7.setMainApp(this);
controller7.update();
break;
case "view/Operations.fxml":
// Give the controller access to the main app.
//System.out.println((boolean)loader.getController());
OperationsController controller8 = loader.getController();
controller8.setMainApp(this);
controller8.update(0);
break;
case "view/GameOver.fxml":
GameOverController controller9 = loader.getController();
controller9.setMainApp(this);
controller9.update();
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Returns the main stage.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public void splash(String s) throws Exception {
final Task questionTask = new Task<Void>() {
@Override
protected Void call() throws InterruptedException {
for(int i=0;i<questions.size();++i){
Thread.sleep(200);
updateProgress(i+1,questions.size());
updateMessage("Generating questions... "+i+"/"+questions.size());
}
Thread.sleep(200);
updateMessage("Starting game.");
return null;
}
};
showSplash(questionTask,() -> showMainStage(s));
new Thread(questionTask).start();
}
private void showMainStage(String s) {
showScreen(s);
}
private void showSplash(Task<?> task,InitCompletionHandler initCompletionHandler) {
SplashScreenController.progressTextProperty.bind(task.messageProperty());
SplashScreenController.loadProgressProperty.bind(task.progressProperty());
task.stateProperty().addListener((observableValue, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
SplashScreenController.loadProgressProperty.unbind();
SplashScreenController.loadProgressProperty.set(1);
primaryStage.toFront();
initCompletionHandler.complete();
} // todo add code to gracefully handle other task states.
});
final Rectangle2D bounds = Screen.getPrimary().getBounds();
primaryStage.show();
}
public interface InitCompletionHandler {
public void complete();
}
public void setStep(int i){
step+=i;
}
public static void main(String[] args) {
launch(args);
}
}
FollowController.java:
public class FollowController {
@FXML
private Label score;
@FXML
private Label time;
@FXML
private Rectangle r1;
@FXML
private Rectangle r2;
@FXML
private Rectangle r3;
@FXML
private Rectangle r4;
@FXML
private Rectangle r5;
@FXML
private Rectangle r6;
@FXML
private Rectangle r7;
@FXML
private Rectangle r8;
@FXML
private Rectangle r9;
private int timerInt, temp = 0;
private ArrayList<Rectangle> rect = new ArrayList<>();
private ArrayList<Integer> tiles;
private int scoreInt = 0;
private Stage stage;
private MainApp mainApp;
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
rect.add(r1);
rect.add(r2);
rect.add(r3);
rect.add(r4);
rect.add(r5);
rect.add(r6);
rect.add(r7);
rect.add(r8);
rect.add(r9);
}
public void setStage(Stage stage) {
this.stage = stage;
}
public void update() throws InterruptedException{
for(int i=0;i<rect.size();++i){
rect.get(i).setFill(null);
}
timerInt = 120;
AnimationTimer timer = new AnimationTimer(){
public void handle(long l){
temp++;
if(temp%60 == 0)
timerInt--;
if(timerInt == 0){
this.stop();
mainApp.showScreen("view/GameOver.fxml");
}
}
};
timer.start();
updatePattern();
}
public void updatePattern() throws InterruptedException{
Random r = new Random();
int difficulty = r.nextInt(6)+3;
tiles = new ArrayList<Integer>();
for(int i=0;i<difficulty;++i){
int tile = r.nextInt(9)+1;
//check for overlapping
if(i!=0){
for(int j = 0; j<tiles.size(); j++){
while(tile==tiles.get(j)){
tile = r.nextInt(9);
j=0;
}
}
}
tiles.add((Integer)tile);
System.out.println("thread tiles "+tiles);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
setRect(tiles);
}
public void setRect(ArrayList<Integer> list) throws InterruptedException{
for(int i = 0; i<list.size(); i++){
rect.get(list.get(i)).setFill(Color.BLUE);
Thread.sleep(1000);
}
}
public void correct(){
scoreInt+=1;
score.setText(Integer.toString(scoreInt));
}
public void checkCorrect(int tile, int i){
if(tile==i)
correct();
}
}