I am trying to make SKAction launch forever, which I can stop when I want. I did it like this:
override func didMove(to view: SKView) { run(SKAction.repeatForever ( SKAction.sequence ([ SKAction.run(drawFrame), SKAction.wait(forDuration: 0.01), ])), withKey: "frameDrawing" ) }
Then, in the drawFrame function, stop SKAction as follows:
func drawFrame() { //(code) if stop { removeAction(forKey: "frameDrawing") } }
For some reason, SKAction stops only when it starts 3 or 4 times after the stop has become true. I want it to stop instantly when stop is set to true, and not after 3 or 4 repetitions.
If anyone knows how to fix this, tell me because I have tried many things and they never fix the problem. Thanks!
Edit: Here is my code:
var drawingFrame = SKAction() class GameScene: SKScene, SKPhysicsContactDelegate { override func didMove(to view: SKView) { drawingFrame = SKAction.repeatForever ( SKAction.sequence ([ SKAction.run(drawFrame), SKAction.wait(forDuration: 0.03), ])) run(drawingFrame) } func drawFrame() {
If you're wondering why I set the SKAction drawing frame to an empty SKAction at the beginning, this is because I needed to define a SKAction in front of both functions. Otherwise, it will not be defined for both functions.
CHANGE ANY Tough Problem: I fixed the problem using my own thinking and @appzYourLife solution. The most efficient way that works every time is to only run the code if the stop is false. But make sure that the if statement that stops the program is outside of this bracket, so SKAction will eventually stop. This is the working code:
var drawingFrame = SKAction() class GameScene: SKScene, SKPhysicsContactDelegate { override func didMove(to view: SKView) { drawingFrame = SKAction.repeatForever ( SKAction.sequence ([ SKAction.run(drawFrame), SKAction.wait(forDuration: 0.03), ])) run(drawingFrame) } func drawFrame() { if stop = false {
If you prefer this, you can use the if else statement for the stop = false statement.