代码之家  ›  专栏  ›  技术社区  ›  kat1234

为什么我的javaFX字段在每个关键帧中都会重置?

  •  0
  • kat1234  · 技术社区  · 1 年前

    我正在尝试使用javaFX编写一个简单的蛇游戏,我目前正在开发这个动作。我使用时间线使蛇移动每个关键帧,然后注册keyEvents以更改方向。但每次我改变方向,该字段都会立即恢复到其初始值。我知道我的代码很粗糙和混乱,这是我第一次正确使用javaFX。我也知道我应该对代码进行更多的分段,但我真的只想让这部分工作。 这个问题可能很大,也很明显,但我是新来的,很愚蠢,有人知道怎么了吗?非常感谢。

    主代码:

    public class Main2 extends Application {
        @FXML
        private Direction dir;
        
        @FXML
        private Canvas canvas;
    
        @FXML
        private Pane pane;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage){
            try {
                Pane root = FXMLLoader.load(getClass().getResource("Test.fxml"));
                primaryStage.setTitle("Snake");
                Scene scene = new Scene(root);
                Rectangle rec = new Rectangle(290,190,10,10);
                rec.setFill(Color.BLACK);
                root.getChildren().add(rec);
                primaryStage.setScene(scene);
                primaryStage.show();  
                root.requestFocus();
                dir = Direction.RIGHT;
                Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.1),e->{
                    
                    System.out.println(dir);
                    
    
    
                }));
                timeline.setCycleCount(Animation.INDEFINITE);
                timeline.play();
                
                
            } catch (Exception IOException) {
                // TODO: handle exception
            }
            
        }
    
        
    
        public void setDirection(Direction d){
            switch (d){
                case UP:
                    dir = Direction.UP;
                    break;
                case DOWN:
                    dir = Direction.DOWN;
                    break;
                case LEFT:
                    dir = Direction.LEFT;
                    break;
                case RIGHT:
                    dir = Direction.RIGHT;
                    break;
                default:
                    break;
    
            }
    
    
        }
    
        @FXML
        void keyPressed(KeyEvent event) {
                    switch (event.getCode()){
                        case RIGHT:
                            setDirection(Direction.RIGHT);
                            System.out.println(this.dir);
                            break;
                        case LEFT:
                            setDirection(Direction.LEFT);
                            System.out.println(this.dir);
                            break;
                        case DOWN:
                            setDirection(Direction.DOWN);
                            System.out.println(this.dir);
                            break;
                        case UP:
                            setDirection(Direction.UP);
                            System.out.println(this.dir);
                            break;
                        default:                        
                            break;
    
    
                    }
        }
    
    
        
    
    }
    

    FXML:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.canvas.Canvas?>
    <?import javafx.scene.layout.Pane?>
    
    
    <Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#keyPressed" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main2">
       <children>
          <Canvas fx:id="canvas" height="380.0" layoutX="9.0" layoutY="10.0" width="582.0" />
       </children>
    </Pane>
    
    0 回复  |  直到 1 年前
    推荐文章