Leaks using GKStateMachine in a Subclass

蓝咒 提交于 2019-12-23 01:50:07

问题


I will show you my actual code. It has three elements: a Helper:

import SpriteKit
import GameplayKit

class GameSceneHelper: SKScene {

   override func didMove(to view: SKView) {

   }
}

A subclass of the helper with some game's states:

import SpriteKit
import GameplayKit

class GameScene: GameSceneHelper {

   lazy var gameState:GKStateMachine = GKStateMachine(states: [
      Introduction(scene: self),
      SecondState(scene: self) ])

   override func didMove(to view: SKView) {

      self.gameState.enter(Introduction.self)
   }
}

And the States. Here I present one of them. The other has the same structure:

import SpriteKit
import GameplayKit

class Introduction: GKState {

   weak var scene:GameScene?

   init(scene:SKScene) {
      self.scene = scene as? GameScene
      super.init()
   }

   override func didEnter(from previousState: GKState?) {
      print("INSIDE THE Introduction STATE")
   }
}

The problem is that I'm getting a Leak when I define the gameState variable inside the subclass of GameSceneHelper. But, If I don't use the subclass and instead I make the GameScene a direct subclass of SKScene, everything works. The problem is that for my project I need the helper so I can't take it out of the equation. Does anybody has any suggestion?


回答1:


Ok. After a lot of time doing this I found the source of the problem. The declaration of the gameState must be moved from GameScene to GameSceneHelper like this:

class GameSceneHelper: SKScene {

   var gameState:GKStateMachine!

   override func didMove(to view: SKView) {
   }
}

No use for the lazy var declaration inside GameScene. Then everything works.



来源:https://stackoverflow.com/questions/41591989/leaks-using-gkstatemachine-in-a-subclass

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