简体   繁体   English

JavaFX的平稳运行和时间表

[英]JavaFX smooth movement and timelines

I've recently started using JavaFX and I'm making a small game with a player being controlled by WASD keys. 我最近开始使用JavaFX,并且正在通过WASD按键控制玩家制作一款小型游戏。 At first I made it so you could move the player by adjusting his x & y coordinates on keyPress but I found that the movement was very rough. 首先,我做到了,这样您就可以通过在keyPress上调整他的x和y坐标来移动播放器,但是我发现移动非常粗糙。 Now I changed things and started using Timelines starting & stopping them when a key is pressed and released. 现在,我更改了内容并开始使用时间轴来启动和停止,并在按下和释放键时停止它们。

The code: 编码:

Timeline timelineW = new Timeline();
Timeline timelineA = new Timeline();
Timeline timelineS = new Timeline();
Timeline timelineD = new Timeline();

 public void createTimeLineW() {
    timelineW.setCycleCount(Timeline.INDEFINITE);
    final KeyValue kv = new KeyValue(player.yProperty(), -Integer.MAX_VALUE);
    final KeyFrame kf = new KeyFrame(Duration.hours(3000), kv);
    timelineW.getKeyFrames().add(kf);
}

public void createTimeLineA() {
    timelineA.setCycleCount(Timeline.INDEFINITE);
    final KeyValue kv = new KeyValue(player.xProperty(), -Integer.MAX_VALUE);
    final KeyFrame kf = new KeyFrame(Duration.hours(3000), kv);
    timelineA.getKeyFrames().add(kf);
}

public void createTimeLineS() {
    timelineS.setCycleCount(Timeline.INDEFINITE);
    final KeyValue kv = new KeyValue(player.yProperty(), Integer.MAX_VALUE);
    final KeyFrame kf = new KeyFrame(Duration.hours(3000), kv);
    timelineS.getKeyFrames().add(kf);
}

public void createTimeLineD() {
    timelineD.setCycleCount(Timeline.INDEFINITE);
    final KeyValue kv = new KeyValue(player.xProperty(), Integer.MAX_VALUE);
    final KeyFrame kf = new KeyFrame(Duration.hours(3000), kv);
    timelineD.getKeyFrames().add(kf);
}

This is the base of the current movement. 这是当前运动的基础。 So upon pressing W the imageView player would get timelineW.play() and upon releasing W it would get timelineW.stop(); 因此,在按下W时,imageView播放器将获得timelineW.play();在释放W时,它将获得timelineW.stop();。 and like so for the other keys. 其他键也一样 The overall reason I made this change was because the movement is more smooth but there are still several bugs. 我做出此更改的总体原因是因为运动更加平稳,但仍然存在一些错误。 Is it possible to even do movement with this? 这样做甚至可以运动吗? Or should I look into alternatives. 还是我应该寻找替代方案。

Thanks on forehand. 正手谢谢。

Typically, you'll want to periodically check for a key and reset the movement Timeline: 通常,您需要定期检查按键并重置移动时间轴:

/** Distance player moves in one "step." */
private static final int movementDistance = 10;

/** Time it takes to move one "step." */
private static final Duration movementTime = Duration.seconds(0.5);

private Timeline xMovement;

private void checkMovementKeys() {
    // ...

    if (xMovement == null) {
        xMovement = new Timeline();
    }

    if (rightMovementKeyPressed) {
        xMovement.getKeyFrames().setAll(
            new KeyFrame(movementTime,
                new KeyValue(player.xProperty(),
                    player.getX() + movementDistance)));
        xMovement.playFromStart();
    }
    if (leftMovementKeyPressed) {
        xMovement.getKeyFrames().setAll(
            new KeyFrame(movementTime,
                new KeyValue(player.xProperty(),
                    player.getX() - movementDistance)));
        xMovement.playFromStart();
    }

    // ...
}

Notice that the cycle count is left at its default, which is one. 请注意,循环计数保留默认值,即1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM