Reputation: 39
I divided working code for 2 files to avoid mess. It worked before, but having all scenes in one class is very unpleasant.
Before when you clicked the sprite it would take you from Menu to Game. THen i extracted code with game group and gamescene to game class.
Now I can see that it says "clicked" when I press the sprite. That means changing scene is working. Problem is that after I divided the files the second scene (game scene) is showing content of first scene instead of its own. probably its not repainting. How can I fix it? Thanks
code of Main class (menu window):
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);
}
}
code of class with game window constructor/method:
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 ;
}
}
Upvotes: 0
Views: 703
Reputation: 9177
In your GameGroup
constructor, you new two local variable rather than initialize your fields.
Changes GameGroup.java from
Group gameGroup = new Group(playerImageView);
Scene gameScene = new Scene(gameGroup, 600, 300);
to
gameGroup = new Group(playerImageView);
gameScene = new Scene(gameGroup, 600, 300);
Upvotes: 2