亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Snake - 按鍵響應時間慢

Snake - 按鍵響應時間慢

鴻蒙傳說 2022-06-04 17:32:01
我做了一個蛇游戲(通過嘗試遵循 youtuber 的代碼),方向由 WASD 控制。但是,當我按下其中一個鍵時,什么也沒有發生。如果我按住它,它會改變方向,但是會有很大的延遲,可能超過一秒鐘。我該如何解決?我查看了我的代碼并將其與我多次遵循的 youtube 代碼進行了比較,但似乎仍然看不出問題所在。這是我第一次制作游戲,所以我對此很陌生。
查看完整描述

2 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

我發現您的代碼與教程中的代碼不同。


if (!moved)

     return;


switch (event.getCode()) {

     .

     .

     .

}

moved = false;

在您的代碼中,缺少 if(!moved) 之后的 return 語句。我試圖將它添加到我的代碼中,然后它對我有用。


希望這可以解決您的問題。干杯馬耳他


查看完整回答
反對 回復 2022-06-04
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

問題是每次KeyFrame觸發時,您都會重置moved標志。這需要用戶在幀之間觸發至少 2 個KEY_PRESSED事件才能獲得改變的方向。


假設您想阻止用戶在第一幀之前改變方向,您應該刪除if條件中的否定。(根據您嘗試使用標志實現的目標,您可能需要不同的修復)。


scene.setOnKeyPressed(event -> {

    if (moved) {


        switch (event.getCode()) {

        case W:

            if (direction != Direction.DOWN)

                direction = Direction.UP;

            break;


        case S:

            if (direction != Direction.UP)

                direction = Direction.DOWN;

            break;

        case A:

            if (direction != Direction.RIGHT)

                direction = Direction.LEFT;

            break;

        case D:

            if (direction != Direction.LEFT)

                direction = Direction.RIGHT;

            break;

        default:

            break;


        }

    }

});

此外,您可以通過使用 aMap并向Direction枚舉添加屬性來改進代碼的一些方面。


public enum Direction {

    UP(0, -1), RIGHT(1, 0), DOWN(0, 1), LEFT(-1, 0);


    private final int dx;

    private final int dy;


    private Direction(int dx, int dy) {

        this.dx = dx;

        this.dy = dy;

    }


    /**

     * Tests, if 2 directions are parallel (i.e. both either on the x or the y axis).<br>

     * Note: Depends on the order of the enum constants

     * @param other the direction to compare with

     * @return true, if the directions are parallel, false otherwise

     */

    public boolean isParallel(Direction other) {

        return ((ordinal() - other.ordinal()) & 1) == 0;

    }

}

在里面KeyFrame


...

double tailX = tail.getTranslateX();

double tailY = tail.getTranslateY();


Node head = snake.get(0);

tail.setTranslateX(head.getTranslateX() + BLOCK_SIZE * direction.dx);

tail.setTranslateY(head.getTranslateY() + BLOCK_SIZE * direction.dy);


moved = true;

...

final Map<KeyCode, Direction> keyMapping = new EnumMap<>(KeyCode.class);

keyMapping.put(KeyCode.W, Direction.UP);

keyMapping.put(KeyCode.S, Direction.DOWN);

keyMapping.put(KeyCode.A, Direction.LEFT);

keyMapping.put(KeyCode.D, Direction.RIGHT);


Scene scene = new Scene(createContent());

scene.setOnKeyPressed(event -> {

    if (moved) {

        Direction newDirection = keyMapping.get(event.getCode());

        if (newDirection != null && !direction.isParallel(newDirection)) {

            direction = newDirection;

        }

    }


});


查看完整回答
反對 回復 2022-06-04
  • 2 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號