I have quite a specific problem. I used to be presenting my Game Scene from a view controller with the default code. However I Added another SKScene and am now presenting my game scene from that using this code:
//In an SKScene (not Game scene)
let scene = GameScene()
let skView = self.view!
skView.ignoresSiblingOrder = true
scene.scaleMode = .aspectFit
let push = SKTransition.push(with: SKTransitionDirection.right, duration: 0.4)
skView.presentScene(scene, transition: push)
My problem is that my game scene seems to no longer recognize my SKS file as when I run the line of code:
//in gamescene
sprite = self.childNode(withName: "sprite") as! SKLabelNode
It doesn't find anything when unwrapping. When I present my SKScene from GameViewController using the code:
if let scene = GKScene(fileNamed: "GameScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! GameScene? {
// Copy gameplay related content over to the scene
sceneNode.entities = scene.entities
sceneNode.graphs = scene.graphs
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFit
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
}
}
}
Everything works. What am I doing wrong? Thanks in advance.
UIViewController
(is this correct?) If so. when you writeself.childNode()...
doesn't self refer to aUIViewController
? Shouldn't it bescene.childNode()
? And how do you add the child node in the first place? – AcoopSKView
within the first one, and then replace theview
? – Acoopself.childNode()
. I assume that this line is in the second scene? Is this right? And where was the sprite originally added? To the first scene? – Acoop