In Swift on “game over” move from scene to another UIView and dispose the scene?

孤街醉人 提交于 2019-12-25 04:36:19

问题


I have a "life counter" in my game and when it hits 0 = dead - i want to move away from the scene to another UIView - which is game over view - with stats and a button to go back to the home screen (first UIViewController with buttons to start the game and so on.

Here is the code of how i move to the Game Over view

class GameScene: SKScene,SKPhysicsContactDelegate {
var viewController: UIViewController?
// more code and functions
// ......

 func trackLife (lifeCHange: Int){
    life = life + lifeCHange
    lifeLabel.text = String(life)
    if life < 1 {
        // Go to Game Over VC
        self.removeAllChildren()
        self.removeAllActions()
        self.scene?.removeFromParent()
        self.viewController!.performSegueWithIdentifier("gameOverSegue", sender: viewController)

    }
  }
}

this works for presenting the game over view, but what i thinks i'm not "disposing" or reseting the scene. because if i do this in a loop:

Start Game --> Game Over --> Back to The Home Screen --> Start Game --> Game Over....

I see the memory usage grows on each cycle:) I guess i'm just adding scenes but not removing them?

I'm sorry - i'm fresh to this. Will be very grateful for your experience!:)


回答1:


To manage your memory effectively in Sprite-Kit, you should create another SKScene for your GameOver screen to be presented from your main screen. In this way, the old SKScene would be released.



来源:https://stackoverflow.com/questions/30423126/in-swift-on-game-over-move-from-scene-to-another-uiview-and-dispose-the-scene

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