SFML community forums

Help => System => Topic started by: vengerSC on May 21, 2022, 07:28:00 pm

Title: sf::clock dont work inside a class
Post by: vengerSC on May 21, 2022, 07:28:00 pm
I'm triyng to use a sf::clock to compare time and use it as an safety lock for my mouse, it work fine on my main script, but on my class it don't, and i don't know why.
I'm using c++.

Title: Re: sf::clock dont work inside a class
Post by: Stauricus on May 21, 2022, 09:00:11 pm
you are creating a new Clock everytime 'RectOnClick' is called. try moving sf::Clock cl1 outside the RectOnClick function.
Title: Re: sf::clock dont work inside a class
Post by: vengerSC on May 21, 2022, 09:16:53 pm
Your reply give me the "brilliant ideia" to add clock as static and it worked well, thank you.
Title: Re: sf::clock dont work inside a class
Post by: Stauricus on May 21, 2022, 10:05:11 pm
what!? no, don't need to add it as static (unless, of course, you want many objects from the class to use the same clock). are you used to program in Python or something? it's different in C++
just move the clock outside the funcion, but still inside the class, something like this:

class Rect {

sf::Clock cl1;

public:

        Rect() {}


        void RectOnClick(int Mousex, int Mousey) {
               
                Lemon_Milk.loadFromFile("C:/Users/Felipe Palermo/source/repos/sfmllearn/sfmllearn/LEMONMILK-Bold.otf");

                static int contagem = 1;
                bool LockClick = false;



                if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && LockClick == false) {
//the rest of the code is the same