SKSprite场景的内存问题

时间:2016-01-06 21:24:54

标签: ios swift memory-management sprite-kit skspritenode

当我尝试在两个场景之间转换时,内存会上升。没有解除分配。我尝试调用skView.removeFromSuperview()但屏幕变黑。这是我的代码:

func goGame()
{

    //crowdImage.hidden = true
    imageView.removeFromSuperview()
    scene?.removeAllActions()
    scene?.removeAllChildren()
    scene?.removeFromParent()
    //view.removeFromSuperview()
    if myviewbool == 0
    {

        if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
            skView = self.view as! SKView
            skView.frameInterval = 2
            skView.ignoresSiblingOrder = false
            scene.viewController = self
            scene.scaleMode = .AspectFill
            skView.presentScene(scene, transition: SKTransition.crossFadeWithDuration(1))
        }
        skView2.scene?.removeFromParent()
        //skView2.removeFromSuperview() // CAUSES BLACK SCREEN
    }
    if myviewbool == 1
    {

        if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
            skView2 = self.view as! SKView
            skView2.frameInterval = 2
            skView2.ignoresSiblingOrder = false
            scene.viewController = self
            scene.scaleMode = .AspectFill
            skView2.presentScene(scene, transition: SKTransition.crossFadeWithDuration(1))

        }
        skView.scene?.removeFromParent()
        //skView.removeFromSuperview() // CAUSES BLACK SCREEN
    }

}

当游戏结束时,我调用func gameEnd()来切换myviewbool变量并调用goGame()。任何帮助深表感谢! ; - )

func gameEnded()
{
    if myviewbool == 0
    {
        myviewbool = 1
    }
    else
    {
        myviewbool = 0
    }
    isGamePlaying = false
    goGame()

}

最终我收到内存警告......然后应用程序崩溃了。

1 个答案:

答案 0 :(得分:0)

因此,经过大量时间花在尝试调试导致内存问题的问题之后,它最终成为了NSTimers。谢谢KnightOfDragon让我走上了正确的道路。虽然我会在加载新场景之前尝试使每个计时器无效,但计时器从未卸载并在后台继续。从未调用deinit函数,因此从未卸载场景。我可以使用的唯一解决方案是根本不使用NSTimers,而是在此处使用此自定义类:https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk1ch12p499timerLeaker2/ch12p325NotificationLeaker/CancelableTimer.swift

希望这可以帮助将来的某个人。这让我很容易花费40个小时的工作时间。 :(