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.
I don't have an exact explanation why drawFrame() is called multiple times at the moment (but I will try to discover that :D)... Anyways, try this code:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var stop = false
override func didMove(to view: SKView) {
run(SKAction.repeatForever (
SKAction.sequence ([
SKAction.run({[unowned self] in
if self.stop {
self.action(forKey: "frameDrawing")?.speed = 0.0
}else{
self.drawFrame()
}
}),
SKAction.wait(forDuration:0.03),
])), withKey: "frameDrawing"
)
}
func drawFrame() {
//(code)
print("drawFrame")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
stop = !stop
print(stop ? "stopped": "running")
if !stop {
self.action(forKey: "frameDrawing")?.speed = 1.0
}
}
}
Use touchesBegan to toggle paused mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With