Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Cannot capture S and A at the same time (sf::Keyboard::isKeyPressed) 2.4.0  (Read 4695 times)

0 Members and 2 Guests are viewing this topic.

emersonalbertdomingo

  • Newbie
  • *
  • Posts: 1
    • View Profile
I'm making a game where the character can move in two directions at once - things like up-left, up-right, down-left, etc. This is how I capture multiple keys.

if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
                //Make a bullet that is going Down at the coordinates of the mouse event

                 
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
                        mainPlayer.playerDirection = direction::down_right;
                }
                else {
                        mainPlayer.playerDirection = direction::down;
                }

This is fairly straightforward code and works quite well! I have one problem though.
down_right, up_right, and up_left all work using this method. The problem occurs here:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
                //Make a bullet that is going Down at the coordinates of the mouse event

                 
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
                        mainPlayer.playerDirection = direction::down_right;
                        cout << "downright";
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
                        mainPlayer.playerDirection = direction::down_left;
                        cout << "down left\n"; //This code NEVER executes. Ever.
                }
                else {
                        mainPlayer.playerDirection = direction::down;
                }

        }

ALL other key combinations work except S and A. I have tried reversing the roles (A first then S), testing different combinations (down arrow and left arrow works just fine). This specific combination of keys does not work for some odd reason. I am stumped and could really use some help. There could be an easy solution staring me in the face, or it may be a bug in 2.4's code. Thanks!

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
are you pressing both the S and the A key at the same time ?

because that's the only way it should work

man'O'war

  • Newbie
  • *
  • Posts: 46
  • What needs to be done is done by those capable.
    • View Profile
I suggest you separate your if ... elses in two blocks,
Trying to move your character up and down at the same time makes no sense, same for right and left.

Each two opposites actions will figure in the same condition-block, its either this or that but never both
And this will permit to detect 'up to 2 keys-pressed simultaneously' (D or A) and (S or W ) or one of them or nothing of course.

However, it may not be suitable for such : playerDirection variable like in your code. unless you split your playerDirection in two, playerDirection.x and playerDirection.y ( sf::vector2 )

Here is the code if you can adapt it:
if( sf::Keyboard::isKeyPressed(sf::Keyboard::D) ) {
        mainPlayer.playerDirection.x = direction::right;
    cout << "right";
}else if( sf::Keyboard::isKeyPressed(sf::Keyboard::A) ){
        mainPlayer.playerDirection.x = direction::left;
    cout << "left";
}

if( sf::Keyboard::isKeyPressed(sf::Keyboard::W) ) {
        mainPlayer.playerDirection.y = direction::up;
    cout << "up";
}else if( sf::Keyboard::isKeyPressed(sf::Keyboard::S) ){
        mainPlayer.playerDirection.y = direction::down;
    cout << "down";
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Are you pressing any other keys at the same time? Some combinations of keys on certain keyboards can cause further keys to not be recognised.

Are you testing anything before this "if S is pressed"? For example, you may be testing to see if A is pressed and then only testing to see if S is pressed when A is not.

@man'O'war, I don't think your solution is particularly player-friendly. Sometimes, a player may press both keys (for example, left and right) at the same time. Expected results could be that they cancel each other out and don't move in either direction, maybe just continue in the direction that was pressed first, or even change direction to the newly pressed key. However, its unintuitive to always travel in a predetermined direction based on which key is detected first without any valid reason. Always travelling right when both left and right are pressed makes no sense.

@emersonalbertdomingo, might I suggest something similar to this:
https://github.com/Hapaxia/BringItBack/blob/b8fab970d1cd01a9979d651e178a824195b49013/functions.cpp#L28-L53
« Last Edit: August 25, 2016, 02:46:03 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

man'O'war

  • Newbie
  • *
  • Posts: 46
  • What needs to be done is done by those capable.
    • View Profile
Quote from: Hapax
@man'O'war, I don't think your solution is particularly player-friendly. Sometimes, a player may press both keys (for example, left and right) at the same time. Expected results could be that they cancel each other out and don't move in either direction, maybe just continue in the direction that was pressed first, or even change direction to the newly pressed key. However, its unintuitive to always travel in a predetermined direction based on which key is detected first without any valid reason. Always travelling right when both left and right are pressed makes no sense.
That is true, first condition prevails.

Quote from: Hapax
Always travelling right when both left and right are pressed makes no sense.
Pressing left & right at the same time either makes no sense, it's is the player's fault. :P

Quote from: Hapax
@emersonalbertdomingo, might I suggest something similar to this:
https://github.com/Hapaxia/BringItBack/blob/b8fab970d1cd01a9979d651e178a824195b49013/functions.cpp#L28-L53
Yes, i see opposite actions are canceled. well done

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Quote from: Hapax
Always travelling right when both left and right are pressed makes no sense.
Pressing left & right at the same time either makes no sense, it's is the player's fault. :P
It's often better to be able to overlap controls than have to force a player to release one direction before pressing a new one. Maybe the best "solution" is to use the most recently pressed direction.

However, I'd argue that there may be occasions where pressing both (opposite) directions can mea something. For example, a game where pressing left and right at the same time means that both sides of the player pull at the same time. This could then create a "longer" character - maybe to be able to catch something :D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

man'O'war

  • Newbie
  • *
  • Posts: 46
  • What needs to be done is done by those capable.
    • View Profile
It's often better to be able to overlap controls than have to force a player to release one direction before pressing a new one. Maybe the best "solution" is to use the most recently pressed direction.
It is definitely related to the game-logic. physic-based games, simple platformer etc. In each case you interpret it accordingly.

Quote from: Hapax
This could then create a "longer" character - maybe to be able to catch something :D
Hahaa.

 

anything