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

Author Topic: How to handle ONLY one event ?  (Read 1751 times)

0 Members and 2 Guests are viewing this topic.

Elfayer

  • Newbie
  • *
  • Posts: 42
    • View Profile
How to handle ONLY one event ?
« on: March 15, 2013, 05:05:05 pm »
Hi,
I would like to handle the input one by one. I know the events are "stacking" in SFML. But if the user is "spamming" his keyboad like a fool, I don't want to stack it. I don't know if it's clear. If Space is used for a jump, if the user spam his Space key, he'll jump again and again when he hit the floor.

And I would like to know how to clear the event i'm handling. For exemple when I use Space to pause the game and I want another Space to leave the pause it, the execution will go too fast, so the last input will be the Space and it will directly leave the pause instantly. Must I have to wait the user to release the Space key and then wait another Space to leave the pause?

Thanks in advance !

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: How to handle ONLY one event ?
« Reply #1 on: March 15, 2013, 05:10:34 pm »
You do already handle the inputs one by one, but at 60+fps it's impossible for us to notice it. ;)

What you basically want is just a processing delay. For that you can for instance use a sf::Clock and whenever the player pressed the spacebar for the first time, you reset the clock and set a boolean to true/false (however you like it). Then before processing any further inputs from the spacebar, you check whether the clock has been running for 1s or 800ms. If not you don't process that input, otherwise you do your second action.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Elfayer

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: How to handle ONLY one event ?
« Reply #2 on: March 15, 2013, 05:14:23 pm »
Ok, I'll look for sf::Clock. But that is not resolving my pause problem.
I'm trying that, but I never go out of the second loop:
If the user press Space if goes on the function :
Quote
{
while (event.type == sf::Event::KeyPressed); // I'm waiting the user to release the space key
while (window.waitEvent(event) && event.key.code != sf::Keyboard::Space); // then waiting him to press space again
}
« Last Edit: March 15, 2013, 05:16:28 pm by Elfayer »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: How to handle ONLY one event ?
« Reply #3 on: March 15, 2013, 05:29:45 pm »
Have you read the official tutorial about events? ::)
Because you're using them completely wrong.

Read it!

You might be also interested in the tutorial  about the input classes. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything