SpriteKit场景过渡的好习惯

时间:2015-04-19 19:24:26

标签: swift sprite-kit out-of-memory skscene sktransition

我正在使用SpriteKit和Swift编写游戏,并且遇到了内存问题。

我的游戏布局是GameViewController(UIViewController)在viewDidLoad屏幕中呈现第一个SKScene(levelChooserScene)。这个场景只是显示一堆按钮。当用户选择按钮时,场景然后使用skView.presentScene转换到正确的场景,当关卡完成后,该场景然后转换回levelChooserScene,游戏就可以让用户选择下一个关卡。

问题是,当转换回levelChooserScene时,为游戏场景分配的内存没有被释放,所以在选择了几个级别之后,我开始接收内存错误。

我的设计在从SKScene过渡到SKScene时是否正确,或者我应该每次都返回到GameViewController,然后从那里过渡到下一个SKScene?

我在这里发现了一些帖子,说我应该在场景之间调用skView.presentScene(nil),但我对如何或在何处实现它感到困惑。

我只是想从一个SKScene转换到另一个SKScene,并将从传出场景中使用的内存返回到系统。

这是我如何实施SKScene的一个例子:

class Level3: SKScene
{
   var explodingRockTimer = NSTimer()
   var blowingUpTheRocks = SKAction()

   override func didMoveToView(view: SKView)
   {
       NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "dismissTheScene:", userInfo: nil, repeats: false)
       var wait = SKAction.waitForDuration(0.5)
       var run = SKAction.runBlock{
           // your code here ...
           self.explodeSomeRocks()
       }
       let runIt = SKAction.sequence([wait,run])
       self.runAction(SKAction.repeatActionForever(runIt), withKey: "blowingUpRocks")

       var dismissalWait = SKAction.waitForDuration(5.0)
       var dismissalRun = SKAction.runBlock{
           self.removeActionForKey("blowingUpRocks")
           self.dismissTheScene()

       }
       self.runAction(SKAction.sequence([dismissalWait,dismissalRun]))
   }

   func explodeSomeRocks()
   {
       println("Timer fired")
   }

   //MARK: - Dismiss back to the level selector
   func dismissTheScene()
   {
       let skView = self.view as SKView?
       var nextScene = SKScene()

       nextScene = LevelChooserScene()
       nextScene.size = skView!.bounds.size
       nextScene.scaleMode = .AspectFill
       var sceneTransition = SKTransition.fadeWithColor(UIColor.blackColor(), duration: 1.5) //WithDuration(2.0)
       //var sceneTransition = SKTransition.pushWithDirection(SKTransitionDirection.Down, duration: 0.75) //WithDuration(2.0)
       //var sceneTransition = SKTransition.crossFadeWithDuration(1.0)
       //var sceneTransition = SKTransition.doorwayWithDuration(1.0)
       sceneTransition.pausesOutgoingScene = true

       skView!.presentScene(nextScene, transition: sceneTransition)
   }
}

2 个答案:

答案 0 :(得分:1)

导致我麻烦的是使用SKAction.repeatActionForever()调用发射器插入函数,每半秒插入粒子发射器5秒钟。

这个repeatAction显然没有被转换到另一个场景而被杀死,并且导致整个场景的内存被保留。我转而使用SKAction.repeatAction(),并指定应该触发的时间。当我转换到新场景时,场景现在返回所有内存。

我不确定我是否理解这种行为。

答案 1 :(得分:1)

SpriteKit在创建复杂游戏时并没有严格记录。我个人经历了几天这样的问题,直到设法解决。

某些对象保留引用,因此不会取消初始化。 (SKAction,计时器等)

在呈现新场景之前,我调用了prepare_deinit()函数,在该函数中,我手动删除了通常不会被swift释放的强引用。

func prepare_deinit()
{
    game_timer.invalidate() // for Timer()
    removeAction(forKey: "blowingUpRocks") // for SKAction in your case

    // I usually add the specific actions to an object and then remove 
    object.removeAllActions()

    // If you create your own object/class that doesn't deinit, remove all object 
    //actions and the object itself
    custom_object.removeAllActions()
    custom_object.removeFromParent()

}

deinit
{
   print("GameScene deinited")
}

我遇到的最后一个问题是,新场景的呈现速度比我的prepare_deinit()快得多,因此我不得不稍后再呈现新场景,从而使prepare_deinit()有足够的时间来释放所有对象。

let new_scene =
{
   let transition = SKTransition.flipVertical(withDuration: 1.0)
   let next_scene = FinishScene(fileNamed: "FinishScene")
   next_scene?.scaleMode = self.scaleMode
   next_scene?.name = "finish"
   self.view?.presentScene(next_scene!, transition: transition)
}

run(SKAction.sequence([SKAction.run(prepare_deinit), SKAction.wait(forDuration: 0.25), SKAction.run(exit_to_finish)]))
相关问题