I'm trying to tidy some of my SKScene code. Currently I have about 11 references to SKNodes (some are layers containing sub-nodes). These nodes and their sub-nodes are frequently accessed by the class. The way I considered doing this is:
Subclass all the SKNodes to another class, for example, currently I have layerPause: SKNode?
defined at the top of the SKScene. This would become layerPause: PauseMenu
where:
class PauseMenu: SKNode
{
lazy var lbBestTime: SKLabelNode = childNodeWithName("lbPersonalBest") as! SKLabelNode
}
Then, when the scene is loading I can simply use:
layerPause = pauseScene.childNodeWithName("pauseMenu")?.copy() as? SKNode
This would allow me to easily access important sub-nodes without calling childNodeWithName
all the time. But unfortunately, the PauseMenu class gives an error saying I can't use childNodeWithName
method.
Could someone point me in the right direction? Maybe there is a more elegant way to manage my nodes, or perhaps I'm missing something simple in the PauseMenu subclass as described above.
Many thanks,
So close, you just need to add an explicit self
:
class PauseMenu: SKNode
{
lazy var lbBestTime: SKLabelNode =
self.childNodeWithName("lbPersonalBest") as! SKLabelNode
}