Reputation: 830
I'm creating a maze game where there will be multiple 'chasers' following a player round a maze, to do this I need to relocate the chasers however i've ran into an issue using .relocate() it only moves it once ,I understand it does this because it shifts the whole coordinates axis itself rather than just the nodes coordinates.
I cannot use chaser.setCentreX() unfortunately to move it as I will be dynamically creating the nodes and will all have the same name 'chaser' hence why I am using getChildren() to get the coordinates. To differentiate between the multiple chasers I will use .setId() .getId() so that I can access the LayoutBounds of a specific node. For example if there were three chasers the first one would be called 'chaser' with and Id of 1 then the next with and Id of 2 etc..
Is there an alternative way of relocating a node without having this issue? , the code below demonstrates the problem.
package sample;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Pane pane=new Pane();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(pane, 600, 600));
primaryStage.show();
Circle chaser=new Circle();
chaser.setRadius(12);
chaser.setCenterX(100);
chaser.setCenterY(50);
chaser.setFill(Color.YELLOW);
pane.getChildren().add(chaser);
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(30), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("running");
pane.getChildren().get(0).relocate(chaser.getCenterX()+400,chaser.getCenterY());
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 1
Views: 1066
Reputation: 82461
What relocate
does is assign the layoutX
and layoutY
properties. Those are completely independent from centerX
and centerY
. Since the latter properties are never updated, the target location remains constant regardless of how often you execute relocate(chaser.getCenterX()+400,chaser.getCenterY());
on chaser
.
To see updates, you need to refer to an value that is updated when updating the location, e.g.
chaser.relocate(chaser.getLayoutX() + 400, chaser.getLayoutY());
or
chaser.setCenterX(chaser.getCenterX() + 400);
// chaser.setCenterY(chaser.getCenterY());
or
chaser.setTranslateX(chaser.getTranslateX() + 400);
// chaser.setTranslateY(chaser.getTranslateY());
As for using ids: That seems completely unnecessary. You could keep track of the chasers without using the CSS id by simply storing them in a suitable data structure. If those are the only children of the parent, you don't even need an extra datastructure, since every chaser would be stored in the children list in that case...
Upvotes: 3