How to dismiss SKScene?

后端 未结 6 1592
迷失自我
迷失自我 2020-12-09 17:30

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

6条回答
  •  不思量自难忘°
    2020-12-09 17:53

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

提交回复
热议问题