Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Execute Code on Key Release?  (Read 12193 times)

0 Members and 1 Guest are viewing this topic.

JCSopko

  • Newbie
  • *
  • Posts: 19
    • View Profile
Execute Code on Key Release?
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Execute Code on Key Release?
« Reply #1 on: June 30, 2010, 06:14:18 pm »
What is a "string of code"?
Laurent Gomila - SFML developer

JCSopko

  • Newbie
  • *
  • Posts: 19
    • View Profile
Execute Code on Key Release?
« Reply #2 on: June 30, 2010, 06:58:49 pm »
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 =]

Spodi

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • http://www.netgore.com/
Execute Code on Key Release?
« Reply #3 on: June 30, 2010, 07:19:50 pm »
Sounds like you need to learn the basics of programming. What you are looking for is conditional statements.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Execute Code on Key Release?
« Reply #4 on: June 30, 2010, 07:20:27 pm »
Quote
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?
Laurent Gomila - SFML developer

JCSopko

  • Newbie
  • *
  • Posts: 19
    • View Profile
Execute Code on Key Release?
« Reply #5 on: June 30, 2010, 07:34:05 pm »
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Execute Code on Key Release?
« Reply #6 on: June 30, 2010, 07:42:25 pm »
Code: [Select]
// 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
Code: [Select]
if (window.GetInput().IsKeyDown(sf::Key::Space))
{
    // apply forces
}
Laurent Gomila - SFML developer

JCSopko

  • Newbie
  • *
  • Posts: 19
    • View Profile
Execute Code on Key Release?
« Reply #7 on: June 30, 2010, 09:22:41 pm »
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:

Code: [Select]
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