I have been trying to make a forever running SKAction that I can stop whenever I want to. I have done 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 I stop the SKAction like this:
func drawFrame() {
//(code)
if stop {
removeAction(forKey: "frameDrawing")
}
}
For some reason the SKAction only stops when it has run 3 or 4 more times after stop became true. I want it to stop instantly when stop is set to true, not after 3 or 4 more repeats.
If anyone knows how to fix this, please tell me because I've tried many things and they never fix the issue. 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() {
//(code)
if stop {
drawingFrame.speed = 0.0
}
}
If you're wondering why I have set the SKAction drawingFrame to an empty SKAction at the start, it is because I needed to define the SKAction before both functions. Otherwise, it would be not defined for both functions.
EDIT FOR ANYONE WITH THE SAME PROBLEM: I have fixed the problem using my own thinking and @appzYourLife's solution. The most efficient way which works every time is to only run the code if stop equals false. But, make sure that the if statement that stops the program is outside of that bracket so the 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 {
//(code)
}
if stop {
removeAllActions()
}
}
You may use an if else statement for the stop = false statement if you prefer that.
stop = true
? Why don't you update your question with a fully workable example because I think what you have posted, actually works as your previous example, means when you set stop = true, multiple drawFrame() calls will be executed right after stopping... – Whirlwind