动画计时器和JavaFX:使用setTranslateX()方法,矩形将不会水平移动.如何移动矩形? [英] AnimationTimer & JavaFX: Rectangle won't move horizontally using the setTranslateX() method. How to move Rectangle?

查看:297
本文介绍了动画计时器和JavaFX:使用setTranslateX()方法,矩形将不会水平移动.如何移动矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个游戏,其中我的Class Rectangle有障碍物,并且使其左右滑动.但是,尽管看起来应该可以,但是我命名为Rectangle1的Rectangle并不会移动.

I am trying to make a game in which I have an obstacle with the Class Rectangle, and have it slide left and right. However, although it seems like it should work, the Rectangle that I named obstacle1 does not move whatsoever.

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);
}



}

我排除了我确定不会影响其他代码的其他代码,因为其他代码仅处理移动其他内容.同样,使用此代码,我的障碍物仍然保持不变.有人知道如何使其滑动吗?

I excluded the other code that I am sure does not affect this, as the other code only deals with moving other things. Again, with this code my obstacle1 stays still. Does anyone know how to make it slide?

推荐答案

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

Your code has (at least) two problems.

  1. obstacle1Position始终为零.
  2. 您假设如果设置了translateX,则x值将发生变化.这不是真的. x值属于几何图形,translateX仅修改其变换.
  1. obstacle1Position is always zero.
  2. You assume that the x-value will change if you set translateX. This is not true. The x-value belongs to the geometry and translateX modifies only its transform.

这篇关于动画计时器和JavaFX:使用setTranslateX()方法,矩形将不会水平移动.如何移动矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆