I usually make my own timer and put it in .update () for things that don't need precise control ... there are Delta-Time actions and functions for that. I will redo this answer easier than the previous one:
Below we create a namespace for several variables, an enumeration timer and use this to update our clock, and also use it as a logical state for whether we need to act when clicking restartButton .
The frame rate is assumed to be 60, so the update is called 60 timers per second. Thus, we add a tick every time, and after 60 ticks we know that there was a second. This is inaccurate, and for games with a frame rate lower than 60 frames, you will need to do some math with delta time in order to correctly set timings (or use a different approach).
But in this simple example, I keep it simple for the main timer, so you can have explicit control over everything in your scene.
Basically, as soon as we reach 3 seconds, it will be time.toPlay = true, making the conditional statement "true", which allows us to execute the click() function that you created. Afterword, it will reset time.toPlay to false, so if you click the button again nothing will happen:
This essentially gives you 3 seconds when you are not allowed to touch the button, as it disappears and nothing. Personally IMO, 2 seconds would be better than 3, especially since your fadein is 1.5 seconds:
// In GameScene field area: enum time { static var ticks = 0 static var seconds = 0 static var toPlay = false } // In update(): time.ticks += 1 if time.ticks >= 60 { time.seconds += 1; time.ticks = 0 } if time.seconds >= 2 { time.seconds = 0; time.toPlay = true } // In the code block where you detect restartButton: if time.toPlay { // Call your restartGame function here: // ... } else { return }
source share