1
Window / two keyboard keys pressed at the same time?
« on: July 22, 2017, 04:46:31 pm »
How do I make my "game" take 2 key inputs, for example if the user clicks w and d he moves up right.
here's my current code:
here's my current code:
while (window.isOpen()) {
Event eventCheck;
while (window.pollEvent(eventCheck)) {
switch (eventCheck.type) {
case Event::Closed:
window.close();
break;
case Event::KeyPressed:
switch (eventCheck.key.code) {
case Keyboard::W:
if (Keyboard::isKeyPressed(Keyboard::A)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x, spritePos.y - 5);
}
break;
case Keyboard::A:
if (Keyboard::isKeyPressed(Keyboard::A)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x - 5, spritePos.y);
}
break;
case Keyboard::S:
if (Keyboard::isKeyPressed(Keyboard::S)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x, spritePos.y + 5);
}
break;
case Keyboard::D:
if (Keyboard::isKeyPressed(Keyboard::D)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x + 5, spritePos.y);
}
break;
}
}
break;
}
window.clear(Color(0,0,0,255));
window.draw(sprite);
window.display();
}
return 0;
Event eventCheck;
while (window.pollEvent(eventCheck)) {
switch (eventCheck.type) {
case Event::Closed:
window.close();
break;
case Event::KeyPressed:
switch (eventCheck.key.code) {
case Keyboard::W:
if (Keyboard::isKeyPressed(Keyboard::A)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x, spritePos.y - 5);
}
break;
case Keyboard::A:
if (Keyboard::isKeyPressed(Keyboard::A)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x - 5, spritePos.y);
}
break;
case Keyboard::S:
if (Keyboard::isKeyPressed(Keyboard::S)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x, spritePos.y + 5);
}
break;
case Keyboard::D:
if (Keyboard::isKeyPressed(Keyboard::D)) {
const Vector2f spritePos = sprite.getPosition();
sprite.setPosition(spritePos.x + 5, spritePos.y);
}
break;
}
}
break;
}
window.clear(Color(0,0,0,255));
window.draw(sprite);
window.display();
}
return 0;