drewvy22
drewvy22

Reputation: 65

Resetting Whole Scene (Swift, SpriteKit)

I'm trying to create a replay button for my game, but whenever I go back to the game scene, It seems as if nothing ever stopped. The time is a negative number and the game will just crash. I tried...

       if timeInt < 0 {


        //////////////
        let retryScene = RetryScene(size: self.frame.size)
        self.view?.presentScene(retryScene)
        self.removeAllChildren()
        self.removeAllActions()

        ///// end game

        timeInt = 45
    }

I figured removing all children would work and resetting the time would work too. I used a function that updates every second to make the time work. So all functions keep going as if the scene never ended. What should I do?

Upvotes: 1

Views: 3428

Answers (2)

drewvy22
drewvy22

Reputation: 65

I fixed it by stopping the timer that makes the update function.

      if time < 0 {
      timer.invalidate()
      }

Upvotes: 0

Darvas
Darvas

Reputation: 974

All the time i whant to restart game I'am presenting Game scene. (starting game scene from begining)

It should look like this

    if (node.name == "ReplayButton") {
        var gameScene = GameScene(size: self.size)
        var transition = SKTransition.doorsCloseHorizontalWithDuration(0.5)
        gameScene.scaleMode = SKSceneScaleMode.AspectFill
        self.scene!.view?.presentScene(gameScene, transition: transition)
    }

Upvotes: 2

Related Questions