Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss SKScene?

When Im finished with my SKScene is there a way to dismiss the SKScene from within my SKScene class?

If not back in my Viewcontroller where I present my SKScene [skView presentScene:theScene]; is there a way to restart the scene or remove in from my SKView?

The SKScene Class Reference and SKView Class Reference are no help.

Update:

The following code removes my scene from my SKView [yourSKView presentScene:nil]; but when Im back in my view controller the scene is still running in the background. I can always pause it when the game is over and I'm sent back to my view controller(menu) but I'm wondering is there another method other then pausing it like completely removing it?

-(void)endTheGame {
    [highscoreLabel removeFromSuperview];
    NSLog(@"Game Over");
   //would like to end here before calling the below method in my view controller
    [self.delegate mySceneDidFinish:self];
}
like image 578
4GetFullOf Avatar asked Feb 20 '14 21:02

4GetFullOf


2 Answers

Having met a similar issue I stumbled around your question, and since nobody gave a decent answer here's how I solved it:

  1. in my scene I called both lines
[self removeFromParent];
[self.view presentScene:nil];
  1. in my controller (the controller that displays the SKScene) I changed the template code from Apple, which was creating and presenting my scene from viewDidLoad in order to create and present my scene in viewWillAppear, only if the view's scene is nil

here's my Swift code, even if you're using Objective C you'll understand what it does; the key line being the "if skView.scene == nil" test :

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    let skView = self.view as SKView
    if skView.scene == nil {
        let scene = GameScene(size:skView.bounds.size)
        scene.controller = self
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .AspectFill
        skView.presentScene(scene)
    }
}
like image 167
Alex Avatar answered Nov 13 '22 19:11

Alex


You can use:

 [yourSKView presentScene:nil];

to remove the scene.

like image 11
AndyOS Avatar answered Nov 13 '22 19:11

AndyOS