为什么 libnds 中的 keyDownRepeat() 在多次调用时似乎不起作用?

发布于 2024-07-09 01:03:47 字数 798 浏览 6 评论 0原文

我有这样的代码来在游戏中向左、向右、向上和向下移动玩家:

keysSetRepeat(20, 5);

while (lives) {
    scanKeys();

    if (keysDownRepeat() & (KEY_LEFT | KEY_RIGHT | KEY_UP | KEY_DOWN)) {
        u8 new_x = x;
        u8 new_y = y;

        if (keysDownRepeat() & KEY_LEFT) {
            new_x--;
        } else if (keysDownRepeat() & KEY_RIGHT) {
            new_x++;
        } else if (keysDownRepeat() & KEY_DOWN) {
            new_y++;
        } else if (keysDownRepeat() & KEY_UP) {
            new_y--;
        }

        // ...
    }

    // ...

    swiWaitForVBlank();
}

为什么没有检测到按键? 如果我用 keysDown() 替换 keysDownRepeat() ,它就可以工作(当然,没有重复率)。 文档在这里没有帮助。

I have code like this to move the player in my game left, right, up, and down:

keysSetRepeat(20, 5);

while (lives) {
    scanKeys();

    if (keysDownRepeat() & (KEY_LEFT | KEY_RIGHT | KEY_UP | KEY_DOWN)) {
        u8 new_x = x;
        u8 new_y = y;

        if (keysDownRepeat() & KEY_LEFT) {
            new_x--;
        } else if (keysDownRepeat() & KEY_RIGHT) {
            new_x++;
        } else if (keysDownRepeat() & KEY_DOWN) {
            new_y++;
        } else if (keysDownRepeat() & KEY_UP) {
            new_y--;
        }

        // ...
    }

    // ...

    swiWaitForVBlank();
}

Why are the keys not being detected? If I replace keysDownRepeat() with keysDown() it works (without the repeat rate, of course). The documentation is no help here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时光的容颜 2024-07-16 01:03:47

我必须找到 libnds 源代码才能解决这个问题。 看一下keysDownRepeat()的实现:

uint32 keysDownRepeat(void) {
    uint32 tmp = keysrepeat;

    keysrepeat = 0;

    return tmp;
}

它实际上返回键然后将它们重置回0。这没有记录。 我通过将 keysDownRepeat() 的结果存储到变量中并使用该变量检查键来解决这个问题:

keysSetRepeat(20, 5);

while (lives) {
    scanKeys();
    u32 keys_down_repeat = keysDownRepeat();

    if (keys_down_repeat & (KEY_LEFT | KEY_RIGHT | KEY_UP | KEY_DOWN)) {
        u8 new_x = x;
        u8 new_y = y;

        if (keys_down_repeat & KEY_LEFT) {
            new_x--;
        } else if (keys_down_repeat & KEY_RIGHT) {
            new_x++;
        } else if (keys_down_repeat & KEY_DOWN) {
            new_y++;
        } else if (keys_down_repeat & KEY_UP) {
            new_y--;
        }

        // ...
    }

    // ...

    swiWaitForVBlank();
}

I had to find the libnds source code to figure this out. Look at the implementation of keysDownRepeat():

uint32 keysDownRepeat(void) {
    uint32 tmp = keysrepeat;

    keysrepeat = 0;

    return tmp;
}

It actually returns the keys then resets them back to 0. This wasn't documented. I solved this by storing the result of keysDownRepeat() into a variable and using the variable to check the keys:

keysSetRepeat(20, 5);

while (lives) {
    scanKeys();
    u32 keys_down_repeat = keysDownRepeat();

    if (keys_down_repeat & (KEY_LEFT | KEY_RIGHT | KEY_UP | KEY_DOWN)) {
        u8 new_x = x;
        u8 new_y = y;

        if (keys_down_repeat & KEY_LEFT) {
            new_x--;
        } else if (keys_down_repeat & KEY_RIGHT) {
            new_x++;
        } else if (keys_down_repeat & KEY_DOWN) {
            new_y++;
        } else if (keys_down_repeat & KEY_UP) {
            new_y--;
        }

        // ...
    }

    // ...

    swiWaitForVBlank();
}
等往事风中吹 2024-07-16 01:03:47

另请注意,您可以使用keysHeld()来识别上一帧中“仍然按住”的键,而keysDown()通常旨在帮助您识别“刚刚在本帧中按下的键”(即,在两个键之间)调用 scanKeys())。 keysDownRepeat() 对于那些想要使用 DPAD 滚动浏览列表的类似键盘行为的人来说显然很有用:每个 X 帧您都会重复看到“再次按下”键。
但不可否认的是,keysDownRepeat() 的语义定义不明确......

Note also that you have keysHeld() to identify keys that are "still hold down" from the previous frame, while keysDown() is typically designed to help you identify "keys that have just beeing pressed this frame" (that is, between two calls of scanKeys()). keysDownRepeat() is obviously useful for people that wants keyboard-like behaviour for scrolling through lists with the DPAD: you'll repeatedly see the key "down again" every X frame.
Admittedly, though, the semantic of keysDownRepeat() is poorly defined ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文