Deallocate SKScene after transition to another SKScene in SpriteKit

后端 未结 5 1855
野的像风
野的像风 2020-12-13 11:09

I have a view controller that has three skscenes as children.

When I transition from one to another, the old skscene doesn\'t get deallocated.

I want it to g

5条回答
  •  时光说笑
    2020-12-13 11:25

    I am just starting out with SpriteKit in Swift and I had the same problem: My intro scene was playing some music that I wanted to stop after transitioning to the main menu scene, and I figured the deinit would be a good place to put the AVAudioPlayer.stop() call in, but deinit was never being called.

    After looking around I learned that it may be because of some strong references to the scene, so in my GameViewController:UIViewController subclass I changed this code:

    let intro = IntroScene(size: skView.bounds.size)
    skView.presentScene(intro)
    

    to

    skView.presentScene(IntroScene(size: skView.bounds.size))
    

    and in the intro scene, that I wanted to be deallocated, I changed

    let mainMenu  = MainMenuScene(size: self.size)
    let crossFade = SKTransition.crossFadeWithDuration(1)
    
    self.scene.view.presentScene(mainMenu, transition: crossFade)
    

    to

    self.scene.view.presentScene(MainMenuScene(size: self.size),
                                 transition: SKTransition.crossFadeWithDuration(1))
    

    and it worked! After the transition was complete the deinit method got called.

    I assume that the outgoing scene was not being deinitialized because there were variables holding references to it.

提交回复
热议问题