The problem appears to be here. You are checking if the 'A' key is pressed, then only checking if the 'S' key is pressed if the 'A' key is not pressed. You are only checking if 'D' is pressed if the 'A' and 'S' keys are not pressed.
Edit: I forgot the important part (I should really proofread, then re-read the OP prior to posting the reply
): The reason you move diagonally upwards. You are always checking if 'W' or 'S' are pressed, and only checking if 'A' or 'D' are pressed if 'S' is not pressed. Replacing the "
if" with "
else if" on line 52 would bring you to the code I added at the bottom.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){//W
sprit.move(0.0, -1);
std::cout << "W\n";
}//closeW
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){//A
sprit.move(-1, 0);
std::cout << "A\n";
}//closeA
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){//S
sprit.move(0, 1);
std::cout << "S\n";
}//closeS
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
sprit.move(1, 0);
std::cout << "D\n";
}
You shouldn't have the "
else if"'s there, just "
if"'s.
For printing the coordinates of the sprite, you could use:
std::cout << sprit.getPosition().x << ", " << sprit.getPosition().y << std::endl;
Edit: To avoid moving diagonally, you could use (see above for how this is slightly different than your code):
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){ //W is pressed
//moving up
}else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){ //W is not pressed, S is pressed
//moving down
}else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){ //W and S not pressed, A is pressed
//moving left
}else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){ //W, S, and A not pressed, D pressed.
//moving right
}
Keep in mind the order that you are checking these key pressed, because that determines which key overtakes another one. For example, using the above:
'D' is pressed, then 'W' is pressed -> moving up.