I´m not sure what´s your problem but i try to help you 8)
*Do you want that the play can walk left and up and the same time?
Then make 2variables one for the x-axis (left/right) and one for the y-axis(up/down) and change the if´s:
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
movementX = LEFT; // movementX for the x-axis
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
movementX = RIGHT;
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
movementY = UP; // movementX for the y-axis
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
movementY = DOWN;
//The same for the second player
//So the Problem in your second code is also fixed...
Btw please use the codetag (its in the second line right a dropdown menu
AlexAUT
Ok i think i got your Problem :)
Make 2functions, one for each player with a return value
enum Movement
{
left,
right,
up,
down
};
// Functions prototypes
Movement EventPlayer1(sf::Event& event);
Movement EventPlayer2(sf::Event& event);
// Game loop
//...
while(window.pollEvent(e))
{
// Close,resize,lostfocus....
movementP1 = EventPlayer1(event);
movementP2 = EventPlayer2(event);
}
//
Movement EventPlayer1(sf::Event& event)
{
// if left is pressed: return left ...
}
Movement EventPlayer2(sf::Event& event)
{
// if Q is pressed: return left...
}
This way you have 2vars wich hold the current movement...
AlexAUT
On each of your Input checks you instantly return, therefore not allowing any further events you might have to be processed. One way I like to handle multiple inputs (especially for complex button presses) is to create a set of booleans
bool p1keyLeft, p1keyRight, p1keyUpt, p1keyDown, p2keyLeft, p2keyRight, p2keyUp, p2keyDown,
This way, once you do your poll event, you can just assign which keys get pressed during this event loop.
while (window.pollEvent(event))
{
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
p1keyLeft = true;
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
p1keyRight = true;
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
p1keyUp = true;
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
p1keyDown = true;
}
Once you have these booleans set, you can use them any way you want, check each one and move your character accordingly. Remember to reset them to false at the beginning of your input though.