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

Author Topic: Putting a time-based delay in an if statement "breaks" my code  (Read 1677 times)

0 Members and 2 Guests are viewing this topic.

DraGun

  • Newbie
  • *
  • Posts: 21
    • View Profile
Sorry if this is the wrong forum, I wasn't sure where to post this. I'm currently using SFML 2.1 and I am creating a user interface. For whatever reason, when I use sf::Clock (specifically clock.getElapsedTime()) it makes the if statement execute at random clicking intervals (it's applied to a button being clicked on.) Here's the if statement and relevant code:

bool back = isSpriteClicked(backButton, mouseCoords, mouse);

                        if (back == true && clock.getElapsedTime() >= sf::milliseconds(150))
                        {
                                backButton.setColor(sf::Color(200, 200, 200));
                                clock.restart();

                                scrollBar.setPosition(WINDOW_WIDTH - 49, 53);
                                reportPic.setPosition(270, 10);

                                choice = 0;
                        }

                        else if (back == false)
                                backButton.setColor(sf::Color(scrollArrowColor));

When I remove the time check, it executes perfectly. The reason I have it is because the "back" button is in the same place across all screen (which is normal) and if I don't have any delay, it will "back" out of multiple screens at once, which I'm trying to avoid, of course. The EXTREMELY strange thing is that same if statement works in 2 other places in my code (aside from the variables being different obviously.) Thanks for taking your time to help.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Putting a time-based delay in an if statement "breaks" my code
« Reply #1 on: July 02, 2015, 06:12:48 pm »
I think the more logical approach here would just be to look for a mouse button pressed event. That way it would only execute once per click. Also, if the exact same code works differently in different parts of your program, then something in the rest of the program is causing that.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Putting a time-based delay in an if statement "breaks" my code
« Reply #2 on: July 02, 2015, 06:57:39 pm »
Is the same clock object being used elsewhere?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DraGun

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Putting a time-based delay in an if statement "breaks" my code
« Reply #3 on: July 02, 2015, 08:23:03 pm »
It's being used in multiple times, in each screen and for the text input, but it's getting reset (clock.restart()) before it goes to another screen.

EDIT: The code I posted is the same way I change from the 3 different screens I have currently. The other 2 work just fine, that's the weird part, cuz it's the same code.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Putting a time-based delay in an if statement "breaks" my code
« Reply #4 on: July 03, 2015, 04:14:36 am »
Is the same clock getting restarted elsewhere?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Putting a time-based delay in an if statement "breaks" my code
« Reply #5 on: July 03, 2015, 06:23:15 am »
Provide a minimal and compilable example.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/