Why don't the @DownRepeat () keys in libnds work when you call it multiple times?

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

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 found? If I replace keysDownRepeat()

with keysDown()

, it works (no re-speed, of course). the documentation doesn't help here.

+1


source to share


2 answers


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

uint32 keysDownRepeat(void) {
    uint32 tmp = keysrepeat;

    keysrepeat = 0;

    return tmp;
}

      



It actually returns the keys and then reverts them back to 0. This has not been documented. I solved this by storing the result keysDownRepeat()

in a variable and using the variable to test 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();
}

      

+2


source


Note that you haveHeld () keys to identify keys that are "still held" from the previous frame, while keysDown () is usually designed to help you identify the "keys that just pressed this frame" (i.e. is, between two calls to scanKeys ()). keysDownRepeat () is obviously useful for people who need keyboard-like behavior to scroll through lists with DPAD: you will see the down key constantly every X frame. True, although the semantics of keysDownRepeat () are poorly defined ...



+1


source







All Articles