Search code examples
swiftxcodesprite-kitgame-developmentskaction

Weird behavior of SKAction on children


Well, my aim is to extract the content of a SKScene and then run some actions to their node:

func loadSceneNodes() {
    let loadedScene = SKScene(fileNamed: "newScene.sks")

    // Reads the content of the "Content" node in the newScene.sks
    let content = loadedScene!.childNode(withName: "Content") as! SKSpritenode

    // I move the content to self
    // (THANKS Knight0fDragon for suggestion)
    content.move(toParent: self)

    // Forces the unpause
    self.isPause = false

    // Try to run a test action on the node
    content.run(.rotate(byAngle: 90, duration: 2.0)) {
        print("DONE")
    }

    print(content.hasActions()) // DEBUG -> it prints TRUE
}

What happens is that I successful read the content and it shows on the scene, but the actions aren't shown even if hasActions() results true.

I found out something very interesting by playing with breakpoints and the debug console. content.hasActions() returns true because all of the SKActions get into a stack:

enter image description here

They seem like freeze because if I assign more actions, they will get stack in the pile one by one without getting run. Only once I quit the breakpoint (or whether I pause and then play the application through xCode) everything is executed. Just like the app "needs" a refresh.


Solution

  • Use moveToParent on your sknode instead of addChild on your parent node to avoid the need to create a copy.

    Ensure the isPaused state is set to false, you may be copying at a time where it is true