Search code examples
swiftsprite-kitskspritenodeskaction

How to load nodes from sks file and then apply action defined in Scene Editor


I have two sks files, one with nodes and another with actions both created in Scene Editor. I want to load nodes and add them to another node and then apply actions to them, something like:

override func sceneDidLoad() {

    let heroScene = SKScene(fileNamed: "Hero")
    let head = heroScene?.childNode(withName: "HeroHead") as! SKSpriteNode
    let testAction = SKAction(named: "HeroTestAction") as! SKAction
    let loop = SKAction.repeatForever(testAction)
    let hero = SKSpriteNode(color: SKColor.white.withAlphaComponent(0) , size: CGSize(width: 50, height: 250))
    let body = SKSpriteNode(color: SKColor.blue, size: CGSize(width: 50, height: 50))

    head.removeFromParent()
    hero.addChild(head)
    hero.addChild(body)
    self.addChild(hero)

    head.run(loop)
    body.run(loop)

}

But action does not apply to head. If i apply loop to body or hero it works.

What I do wrong? Thanks!

enter image description here

enter image description here

Update: When I first time run example, action not works, but if close app and run again it is works.

enter image description here


Solution

  • I could want to say there is an interesting "feature" where when you grab a reference sprite like that it comes in as isPaused = true;

    Try adding this

    head.isPaused = false;
    

    I believe I ran into a similar problem before when grabbing a sprite from another scene.