为什么 libnds 中的 keyDownRepeat() 在多次调用时似乎不起作用?
我有这样的代码来在游戏中向左、向右、向上和向下移动玩家:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我必须找到 libnds 源代码才能解决这个问题。 看一下keysDownRepeat()的实现:
它实际上返回键然后将它们重置回0。这没有记录。 我通过将 keysDownRepeat() 的结果存储到变量中并使用该变量检查键来解决这个问题:
I had to find the libnds source code to figure this out. Look at the implementation of keysDownRepeat():
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:另请注意,您可以使用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 ...