为什么在ImageView节点上使用AnimationTimer和setTranslate会产生抖动?

时间:2017-06-13 01:20:31

标签: java android animation javafx imageview

我的目标是使用Java Fx库创建一个基本的2D游戏。我的子目标是创建响应控件和角色的平滑运动。我的问题是当我在其上使用setTranslate时,我的游戏中的玩家(ImageView节点)有点生涩。它似乎在连续运动的每一秒都晃动。这篇文章中包含了一个例子。我已经完成了研究,并且我得到了相互矛盾的答案......例如,有人告诉我使用WritableImage(因为他说它就像BufferedImage),但这似乎并没有解决生涩问题。任何帮助都将非常感激。

更新:感谢您的回复。我发现了问题所在。我使用一个名为f.lux的程序,可以根据一天中的时间自动调整屏幕亮度,并且由于某种原因它会极大地影响游戏的性能。

package pleasehelp;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;

import javafx.stage.Stage;

public class PleaseHelp extends Application {

    ImageView imageView;

    public void start(Stage stage) {

        Pane root = new Pane();

        Scene scene = new Scene(root, 800, 200);

        stage.setTitle("Please Help!");

        stage.setScene(scene);

        Animation animation = new Animation(this);

        Image image = new Image("http://lessonpix.com/drawings/192/100x100/Gray+Square.png");
        imageView = new ImageView();
        imageView.setImage(image);
        root.getChildren().add(imageView);
        animation.start();

        stage.show();

    }

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

}

package pleasehelp;

import javafx.animation.AnimationTimer;

public class Animation extends AnimationTimer {

    PleaseHelp please;

    Animation(PleaseHelp please) {
        this.please = please;
    }

    @Override
    public void handle(long now) {
        please.imageView.setTranslateX(please.imageView.getTranslateX() + 1);
    }

    public void start() {
        super.start();
    }

    public void stop() {
        super.stop();
    }

}

1 个答案:

答案 0 :(得分:0)

在编写这样的动画时,通常最好根据给定时间(现在很长时间)计算下一个位置,而不是仅向当前位置添加常量值。这将使你的动画更具预测性。