Я пытаюсь разработать приложение для игры на игровых автоматах. У меня есть кастомная панель, которая добавляет детей по вертикали. Когда нажимается кнопка прокрутки, дочерние элементы должны двигаться, а когда последний дочерний элемент достигает границы, он должен сместить свою позицию над первым дочерним элементом. То, что я сделал, показано ниже.
public class ReelPane extends Pane {
Timeline timeline = new Timeline();
@Override
protected void layoutChildren() {
List<Node> managed = getChildren();
double y = 0;
for (Node node : managed) {
node.setLayoutX(0);
node.setLayoutY(y);
y += node.getBoundsInLocal().getHeight();
}
}
public void spin() {
List<Node> managed = getChildren();
double dy = 4;
for (Node node : managed) {
timeline.getKeyFrames().addAll(new KeyFrame(Duration.millis(2000),new KeyValue(node.layoutYProperty(),node.getLayoutY()+dy)));
if(node.getLayoutY()>=600){
node.setLayoutY(-50);
}
}
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
}
f xml file
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.Pane?>
<?import sample.ReelPane?>
<Pane stylesheets="@css/slot.css"
xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx"
fx:controller="sample.Controller">
<ReelPane fx:id="reel" styleClass="container">
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/apple.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/diamond.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/glass.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/grape.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/star.png"/>
</ImageView>
</ReelPane>
<Button fx:id="spin" text="SPIN"/>
</Pane>
controller
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class Controller {
@FXML
ReelPane reel;
@FXML
Button spin;
public void initialize() {
spin.setOnAction(event -> reel.spin());
}
}
main
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("resources/fxml/slot.fxml"));
primaryStage.setScene(new Scene(root, 400, 900));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Но дети не двигаются при нажатии кнопки. Может кто-нибудь сказать, что пошло не так?