【问题标题】:AnimationTimer & JavaFX: Rectangle won't move horizontally using the setTranslateX() method. How to move Rectangle?AnimationTimer & JavaFX:矩形不会使用 setTranslateX() 方法水平移动。如何移动矩形?
【发布时间】:2019-01-12 22:29:24
【问题描述】:

我正在尝试制作一个游戏,其中我有一个矩形类障碍物,并让它左右滑动。然而,虽然它看起来应该可以工作,但我命名为障碍物 1 的 Rectangle 并没有移动。

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class SemiCircleMan extends Application {


public Pane pane = new Pane();
public AnimationTimer animate;
public double obstacle1Position = 0;
public int obstacle1Direction = 1;

public void start(Stage primaryStage) {

Scene scene = new Scene(pane, 800, 600);

Rectangle obstacle1 = new Rectangle(100, 125, 25, 25);
    pane.getChildren().add(obstacle1);
    obstacle1Position += 3 * obstacle1Position;

animate = new AnimationTimer() {

        @Override
        public void handle(long now) {

if (obstacle1.getX() <= 500) {
                obstacle1.setTranslateX(obstacle1Position); //attempt to only make it go right
            }

};
    animate.start();


    primaryStage.setScene(scene);
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}



}

我排除了我确信不会影响这一点的其他代码,因为其他代码只处理移动其他事物。同样,使用此代码,我的障碍物1 保持不动。有谁知道如何让它滑动?

【问题讨论】:

  • 您是否尝试过使用setX()setY()docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/…
  • 我尝试复制并粘贴您的代码。它似乎不起作用。
  • 我猜你在这方面付出的努力很少。
  • obstacle1.getX() >= 500 曾经吗?
  • 障碍物1的X位置从100开始,小于等于500,这里是我把代码复制错了,但是用"="。如果你愿意,我可以发布整个游戏代码。

标签: java javafx java-8


【解决方案1】:

您的代码有(至少)两个问题。

  1. obstacle1Position 始终为零。
  2. 您假设如果您设置 translateX,x 值将会改变。这不是真的。 x 值属于几何体, translateX 仅修改其变换。

【讨论】:

    【解决方案2】:

    展示如何使用AnimationTimer 移动Rectangle 的示例应用。您在代码中没有做的一件事是获取Rectangle's 当前位置并从那里移动。

    你的情况

    rectangle.setTranslateX(rectangle.getTranslateX() + direction * speed);
    

    示例应用中的等效代码

    rectangle.setX(rectangle.getX() + direction * speed);
    

    完整代码:

    import javafx.animation.AnimationTimer;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    
    /**
     *
     * @author blj0011
     */
    public class JavaFXApplication235 extends Application
    {
    
        double speed = 3;
        double direction = 1;
    
        @Override
        public void start(Stage primaryStage)
        {
            Rectangle rectangle = new Rectangle(100, 125, 25, 25);
    
            AnimationTimer animationTimer = new AnimationTimer()
            {
                @Override
                public void handle(long now)
                {
                    rectangle.setX(rectangle.getX() + direction * speed);
                }
            };
            animationTimer.start();
    
            Pane root = new Pane(rectangle);
            Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            launch(args);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 2014-12-02
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      相关资源
      最近更新 更多