SFML community forums
Help => Window => Topic started by: JCSopko on June 30, 2010, 05:54:03 pm
-
I've been looking around the forums and the documentation but have been unable to find a way to execute a string of code on the release of a key press with SFML 1.6. Can someone help me out or point me in the right direction? Thanks.
Regards,
Joe
-
What is a "string of code"?
-
My bad, poor wording on my part, a few lines of code like setting a velocity variable to zero. So just replace a string of code with code, sorry for the confusion =]
-
Sounds like you need to learn the basics of programming. What you are looking for is conditional statements.
-
My bad, poor wording on my part, a few lines of code like setting a velocity variable to zero. So just replace a string of code with code, sorry for the confusion =]
Ok :)
So what's your problem exactly? Did you read the tutorial about events first?
-
Yeah I've read through the tutorial and searched through the documentation but have been unable to find exactly what I'm looking for.
I'm using Box2D and am applying a force on key presses but I would like the linear velocity to be set to 0 when the key is released so the player is only moving when the key is held down.
Does SFML allow for this or is there a workaround?
Thanks,
Joe
-
// This is the event loop as described in the tutorials
sf::Event event
while (window.GetEvent(event))
{
if (event.Type == sf::Event::KeyReleased && event.Key.Code == sf::Key::Space)
{
// set linear velocity to 0
}
}
If you're rather applying your forces every frame as long as the key is held, you can use sf::Input instead
if (window.GetInput().IsKeyDown(sf::Key::Space))
{
// apply forces
}
-
I think I understand, but I have work so I don't have time to be sure or try to implement it, I'll try when I get back. Currently my code for momvement is as follows:
if(Game.GetInput().IsKeyDown(sf::Key::A))
{
if(myBox.body->GetLinearVelocity().x > 0)
{
myBox.yVel = myBox.body->GetLinearVelocity().y;
myBox.body->SetLinearVelocity(b2Vec2(0,myBox.yVel));
}
myBox.body->ApplyForce(b2Vec2(-75, 0), b2Vec2(myBox.position.x, myBox.position.y));
}
and that along with other input is in the game loop. so If i put the first example you showed me along with this code in the while loop it should stop all movement on the key release correct?
Thanks for all the help by the way, sorry this post is a little vague and rushed.
Joe