SKAction not running on SKSpriteNode

Viewed 1253

The SKAction on my SKShapeNode isn't working, the code isn't getting executed, and the node isn't moving, even though the console is logging "Snake is moving". Is is because the node is a property of the SKScene and the actions are part of lower scope functions?

class LevelScene: SKScene, SnakeShower {

    var snake: Snake {
        let theSnake = Snake(inWorld: self.size)
        return theSnake
    }

    override func didMove(to view: SKView) {
        self.backgroundColor = .green
        snake.delegate = self
    }

    var myNode: SKShapeNode {
        let node = SKShapeNode(rectOf: snake.componentSize)
        node.position = snake.head
        node.fillColor = .red
        return node
    }

    func presentSnake() { // function called by the snake in the delegate (self)
        self.addChild(myNode)
        startMoving()
    }

    func startMoving() {
        print("snake is moving")
        myNode.run(SKAction.repeatForever(SKAction.sequence([
            SKAction.move(by: self.snake.direction.getVector(), duration: 0.2),
            SKAction.run({
                if self.myNode.position.y > (self.size.height / 2 - self.snake.componentSize.height / 2) {
                    self.myNode.removeAllActions()
                }
            })
        ])))
    }
}

It used to work when they property was declared in the same function as the action

2 Answers

I had an issue almost identical to this, where I had unresponsive child nodes in a referenced node. Turns out that referenced nodes default to isPaused being true (answer thread found here)

The solution was to change the isPaused property to false.

referencedNode.isPaused = false
Related