swift - SKScene returns nil when transition method is called -


in little xcode project trying transition scenes when sklabelnode touched presents next scene. reason in scene transition method makes me store optional. , optional returns false. file names correct , no grammatical errors in references causing problem here code. when click sklabelnode ios platform running on recognizes touch app crashes, in console saying optional value returned nil. how resolve problem? thanks

import spritekit  class gamescene: skscene {  let next = skscene(filenamed: "nextscene")  override func didmovetoview(view: skview) {       let backgroundimage = skspritenode(imagenamed: "ipbg")     backgroundimage.size = cgsize(width: self.frame.size.width, height: self.frame.size.height)     backgroundimage.position = cgpoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)     addchild(backgroundimage)      let playbutton = sklabelnode(fontnamed: "")     playbutton.name = "play"     playbutton.position = cgpoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2 + 100)     playbutton.text = "play"      let wait = skaction.waitforduration(2)     let run = skaction.runblock({           let randomnumber = int(arc4random_uniform(uint32(4)))          switch(randomnumber){          case (0):              playbutton.fontcolor = uicolor.bluecolor()          case 1:              playbutton.fontcolor = uicolor.yellowcolor()          case 2:              playbutton.fontcolor = uicolor.purplecolor()          case 3:              playbutton.fontcolor = uicolor.orangecolor()          default: print("default")           }      })      addchild(playbutton)     var repeatactionforever = skaction.repeatactionforever(skaction.sequence([wait, run]))     runaction(repeatactionforever) }  override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) {      let trans = sktransition.crossfadewithduration(1)     let touch = touches.first! uitouch     let touchlocation = touch.locationinnode(self)     let touchednode = nodeatpoint(touchlocation)      if touchednode.name == "play"{           scene!.view?.presentscene(next!, transition: trans)          }      } }  func update(currenttime: cftimeinterval) {     /* called before each frame rendered */ } 

the code posted not compile it's hard find error.

however try replacing touchesbegan method following

override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) {      let trans = sktransition.crossfadewithduration(1)     let touch = touches.first! uitouch     let touchlocation = touch.locationinnode(self)     let touchednode = nodeatpoint(touchlocation)      if touchednode.name == "play"{         guard let nextscene = skscene(filenamed: "nextscene")             else { fatalerror("could not load nextscene") }         self.view?.presentscene(nextscene, transition: trans)     } } 

Comments

Popular posts from this blog

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

wordpress - (T_ENDFOREACH) php error -

Using django-mptt to get only the categories that have items -