swift - SKScene presented from another SKScene not able to load from SKS file -


i have quite specific problem. used presenting game scene view controller default code. added skscene , presenting game scene using code:

//in 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 game scene seems no longer recognize sks file when run line of code:

//in gamescene sprite = self.childnode(withname: "sprite") as! sklabelnode 

it doesn't find when unwrapping. when present skscene gameviewcontroller using code:

if let scene = gkscene(filenamed: "gamescene") {          // skscene loaded gkscene         if let scenenode = scene.rootnode as! gamescene? {              // copy gameplay related content on scene             scenenode.entities = scene.entities             scenenode.graphs = scene.graphs              // set scale mode scale fit window             scenenode.scalemode = .aspectfit              // present scene             if let view = self.view as! skview? {                 view.presentscene(scenenode)                  view.ignoressiblingorder = true             }         }     } 

everything works. doing wrong? in advance.

i think reason crashes when present within skscene, , not uiviewcontroller because of 2 reasons.

  1. you creating 2 different classes, have different code. when present skscene, create object of type gamescene when present uiviewcontroller create object of type gkscene.
  2. when create object of type gamescene, use initializer not take account attached sks file. when create object of type gkscene do.

therefore, edit first block of code be:

//in skscene (not game scene) if let scenecontainer = gkscene(filenamed: "gamescene") {     let skview = self.view!     skview.ignoressiblingorder = true     //something comparable following line. don't have project set up, let compiler run through exact syntax.     let scene = scenecontainer.rootnode as! gamescene?     scene?.scalemode = .aspectfit     let push = sktransition.push(with: sktransitiondirection.right, duration: 0.4)     skview.presentscene(scene!, transition: push) } 

the doc getting skscene gkscene can found here.


Comments