Search code examples
swiftsprite-kitenumerate

Trying to remove my restart button once it is game over


I am trying to get rid of the restart button once the player has lost 5 lives and it is GAMEOVER I have created. This code is not doing it for me, can anyone help me understand why?

func loseALife(){
    if lives == 0 {
        enumerateChildNodes(withName: "Restart", using: ({
            (node, error) in
                self.removeAllActions()
        })

        runGameOver()
    }
}

This is the function for the restart button

func createButton() {
    restartButton = SKSpriteNode(imageNamed: "restart")
    restartButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    restartButton.zPosition = 6
    restartButton.setScale(0.5)
    restartButton.name = "Restart"
    self.addChild(restartButton)

Solution

  • Instead of self.removeAllActions() use

    node.removeFromParent()
    

    You are using self which in this case refers to the GameScene and removeAllActions which only applies to game elements that currently have actions running. The variable node is from each iteration of enumerateChildNodes and is a direct reference to the button.