SFML community forums
Help => Graphics => Topic started by: Zephilinox on March 15, 2012, 10:21:04 pm
-
I'm trying to get a functioning ping pong game working in sfml2, despite reading and re-reading the documentation, I can't figure out how to find if a key was released, so that I can stop individual paddles from moving.
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
case sf::Event::MouseButtonPressed:
Ball.setPosition(sf::Mouse::getPosition(Window).x - Texture1.getWidth() / 2, sf::Mouse::getPosition(Window).y - Texture1.getHeight() / 2); // test, disable for release
break;
case sf::Event::KeyPressed:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{IsMovingUp1 = true; IsMoving1 = true;}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{IsMovingUp1 = false; IsMoving1 = true;}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{IsMovingUp2 = true; IsMoving2 = true;}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{IsMovingUp2 = false; IsMoving2 = true;}
break;
case sf::Event::KeyReleased: // need to figure out a way of knowing which key it was so I don't stop both paddles
IsMoving1 = false;
IsMoving2 = false;
break;
default:
break;
}
-
Everything is explained in the tutorial.
-
what tutorial? I didn't think sfml2 had any tutorials.
-
Look in the tutorials for 1.6 or whatever. It's pretty easy to deduce any changes and figure it all out, there really isn't much/if any modified from 1.6 to 2.0 in way of input.
-
I've already looked at that, and saying the input hasn't changed really isn't true, its completely different, I've tried adapting it based on the documentation and random attempts, but I just can't get it functional, the closest I could get is this (i.e actually compile), but it stops both paddles regardless.
case sf::Event::KeyReleased: // need to figure out a way of knowing which key it was so I don't stop both paddles
if (sf::Keyboard::W || sf::Keyboard::S)
IsMoving1 = false;
if (sf::Keyboard::Up || sf::Keyboard::Down)
IsMoving2 = false;
break;
all I need is a simple, single, line of code, that I can check to see if a key has been released.
-
If you're trying to get asynchronous input, use, for example:
"sf::Keyboard::IsKeyPressed(sf::Keyboard::A)"
As for the event keypresses, it detects when a key is pressed, I imagine it sets an internal flag saying the key is pressed, then will not trigger again until the key is released, the flag reset, and key pressed again. So you really don't need to do any input management yourself.
-
There's also the online doc, the official examples, the forum, etc...
But anyway, here is the code:
case sf::Event::KeyReleased: // need to figure out a way of knowing which key it was so I don't stop both paddles
if (Event.key.code == sf::Keyboard::W || Event.key.code == sf::Keyboard::S)
IsMoving1 = false;
if (Event.key.code == sf::Keyboard::Up || Event.key.code == sf::Keyboard::Down)
IsMoving2 = false;
break;
-
There's also the online doc, the official examples, the forum, etc...
But anyway, here is the code:
case sf::Event::KeyReleased: // need to figure out a way of knowing which key it was so I don't stop both paddles
if (Event.key.code == sf::Keyboard::W || Event.key.code == sf::Keyboard::S)
IsMoving1 = false;
if (Event.key.code == sf::Keyboard::Up || Event.key.code == sf::Keyboard::Down)
IsMoving2 = false;
break;
thanks, that works perfectly.