0
\$\begingroup\$

I'm having trouble with key management. Movement works fine but the shooting does stuff that I don't want to happen.

Every time I press the arrow keys to shoot it works fine but when I press a key to move the shooting stops and then continues shooting in the direction that I'm pressing.

void Player::HandleEvents() {

    SDL_Event event;
    const Uint8* keyboard_state_array = SDL_GetKeyboardState(NULL);

    while (SDL_PollEvent(&event)) {
        switch (event.type) {
        case SDL_KEYDOWN:

            //Shooting
            if ((keyboard_state_array[SDL_SCANCODE_UP])) {
                shootDirectionY = -1;
            }
            else if ((keyboard_state_array[SDL_SCANCODE_DOWN])) {
                shootDirectionY = 1;
            }
            else if ((keyboard_state_array[SDL_SCANCODE_LEFT])) {
                shootDirectionX = -1;
            }
            else if ((keyboard_state_array[SDL_SCANCODE_RIGHT])) {
                shootDirectionX = 1;
            }

            //Movement
            //Horizontal and Vertical

            if (keyboard_state_array[SDL_SCANCODE_W]) {
                speedSenseX = 0;
                speedSenseY = -1;
            }
            else if (keyboard_state_array[SDL_SCANCODE_S]) {
                speedSenseX = 0;
                speedSenseY = 1;
            }
            else if (keyboard_state_array[SDL_SCANCODE_A]) {
                speedSenseX = -1;
                speedSenseY = 0;
            }
            else if (keyboard_state_array[SDL_SCANCODE_D]) {
                speedSenseX = 1;
                speedSenseY = 0;
            }

            //Diagonals and AD WS cases

            if (keyboard_state_array[SDL_SCANCODE_W] && keyboard_state_array[SDL_SCANCODE_D]) {
                speedSenseX = 1;
                speedSenseY = -1;
            }
            else if (keyboard_state_array[SDL_SCANCODE_W] && keyboard_state_array[SDL_SCANCODE_A]) {
                speedSenseX = -1;
                speedSenseY = -1;
            }
            else if (keyboard_state_array[SDL_SCANCODE_S] && keyboard_state_array[SDL_SCANCODE_D]) {
                speedSenseX = 1;
                speedSenseY = 1;
            }
            else if (keyboard_state_array[SDL_SCANCODE_S] && keyboard_state_array[SDL_SCANCODE_A]) {
                speedSenseX = -1;
                speedSenseY = 1;
            }
            if (keyboard_state_array[SDL_SCANCODE_S] && keyboard_state_array[SDL_SCANCODE_W]) {
                speedSenseX = 0;
                speedSenseY = 0;
            }
            else if (keyboard_state_array[SDL_SCANCODE_A] && keyboard_state_array[SDL_SCANCODE_D]) {
                speedSenseX = 0;
                speedSenseY = 0;
            }
            break;

        case SDL_KEYUP:
            //Handeling speeds
            if (!keyboard_state_array[SDL_SCANCODE_W] && speedSenseY < 0) {
                speedSenseY = 0;
            }
            if (!keyboard_state_array[SDL_SCANCODE_S] && speedSenseY > 0) {
                speedSenseY = 0;
            }
            if (!keyboard_state_array[SDL_SCANCODE_A] && speedSenseX < 0) {
                speedSenseX = 0;
            }
            if (!keyboard_state_array[SDL_SCANCODE_D] && speedSenseX > 0) {
                speedSenseX = 0;
            }
            if (!keyboard_state_array[SDL_SCANCODE_W] && keyboard_state_array[SDL_SCANCODE_S]) {
                speedSenseY = 1;
            }
            if (keyboard_state_array[SDL_SCANCODE_W] && !keyboard_state_array[SDL_SCANCODE_S]) {
                speedSenseY = -1;
            }
            if (!keyboard_state_array[SDL_SCANCODE_A] && keyboard_state_array[SDL_SCANCODE_D]) {
                speedSenseX = 1;
            }
            if (keyboard_state_array[SDL_SCANCODE_A] && !keyboard_state_array[SDL_SCANCODE_D]) {
                speedSenseX = -1;
            }

            break;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ You don't seem to ever handle the key up of the arrow keys. Wouldn't that cause them to continue shooting? \$\endgroup\$ – user1118321 Jun 29 '18 at 5:39
  • \$\begingroup\$ no if they're not pressed no bullets come out, when I press I start shooting \$\endgroup\$ – Artur Díez Jun 29 '18 at 14:41
2
\$\begingroup\$

You're mixing two methods of input detection SDL provides. There is the event API which lets you get input via events, and there is the keyboard state API, which allows you to get the state of the entire keyboard with a single function and query it later. You're mixing them, and it's not working well.

In your code, you're detecting an SDL_KEYDOWN event but then you're querying the state of the entire keyboard in an if/else chain. If you're pressing up, for example, then you'll never be able to detect left, right or down because you're ignoring the information given in the event.

I would stick to just querying the keyboard without events and fix your if/else chains. The code would look something like this. These 4 lines of code should do everything your code was doing.

while (SDL_PollEvent(&event)) {
    switch (event.type) {
        // Handle non-input stuff here like SDL_QUIT
        // Since you're using the other API, ignore keyboard events
    }
}

const Uint8* kb = SDL_GetKeyboardState(NULL);

shootDirectionY = -kb[SDL_SCANCODE_UP] + kb[SDL_SCANCODE_DOWN];
shootDirectionX = -kb[SDL_SCANCODE_LEFT] + kb[SDL_SCANCODE_RIGHT]

moveDirectionY = -kb[SDL_SCANCODE_W] + kb[SDL_SCANCODE_S];
moveDirectionX = -kb[SDL_SCANCODE_A] + kb[SDL_SCANCODE_D];
\$\endgroup\$
0
\$\begingroup\$

You are handling KEYDOWN for shoot direction, but you never handle KEYUP for the same keys?

I'm not sure how you are handling state for your events and I'm not real clear from your question what behaviour your are expecting versus what behaviour you are observing.

Maybe try searching for where else you modify the shoot direction states?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.