How to dismiss SKScene?

谁说我不能喝 提交于 2019-11-28 08:26:30

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)
    }
}

You can use:

 [yourSKView presentScene:nil];

to remove the scene.

"You can't go "Back to the View Controller" from a scene. The scene is a View, the view controller controls and displays views. Use the view controller to change views. Remember the view controller itself is not a view." -Wharbio

Best solution here is to create another View Controller. This view controller will be my menu. Then the other viewcontroller will act as a host for the skscene.

In my situation I then use my menu viewcontroller to dismiss the viewcontroller displaying in the skview.

From within your SKScene, you can simply do [self.view presentScene:aNewScene] to present another scene

I completely remove scenes by using this block in my view controller: Obviously you will need to declare your Size, and "newSKview"

SKView * skView = (SKView *)self.view;

SKScene *newScene = [[newSKView alloc]initWithSize:size];
newScene.scaleMode = SKSceneScaleModeAspectFill;
SKScene *oldScene=(skView.scene);
[oldScene removeFromParent];
[skView presentScene:newScene];

This works fine for me. None of my Scenes are retained or strong.

Romchas

Maybe my variant will be helpful:

[[self.scene childNodeWithName:@"YourChildNodeName"] removeFromParent];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!