MeV
MeV

Reputation: 3968

How can i restart a SKScene?

I am creating a game. When the game is over I am showing a button "Play again" which, ideally, should just reload the Scene.

var viewSize = currentScene.view?.bounds.size
let sceneG = Level1(size: viewSize!)
currentScene.view?.presentScene(sceneG)

but unfortunately, I am getting this error:

Attemped to add a SKNode which already has a parent:

Because some nodes already exist in the Scene and is not possible to add them again.

Is there a way to reload the scene or removing all nodes before presenting the scene again?

Thank you


As requested, this is the Level1 class:

import SpriteKit
import CoreMotion

class Level1: SKScene {

  override func didMoveToView(view: SKView) {
   //creation of nodes etc
  }
}

Upvotes: 3

Views: 1829

Answers (1)

sangony
sangony

Reputation: 11696

If you are looking for a way to remove all children of a scene, use this func removeAllChildren().

Look at the SKNode docs under Working with Node Trees.

Upvotes: 1

Related Questions