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:
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;
well...using event for it can be tough but I think it can be done...I'll try to experiment on it
in the mean time
while (window.isOpen())
{
sf::Event eventCheck;
while (window.pollEvent(eventCheck))
{
if (event.type == sf::Event::Closed) window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
sprite.move(0, -5);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
sprite.move(-5, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
sprite.move(0, 5);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
sprite.move(5, 0);
}
window.clear();
window.draw(sprite);
window.display();
}
return 0;