ios - Why does SpriteKit crash when transitioning back and forth between scenes -
i checked answer here , on other sites didn't definitive solution.
i have 2 scenes
scene 1:
class gamemenuscene: skscene { override func didmovetoview(view: skview) { // add background var background: skspritenode = skspritenode(imagenamed: "starfield") background.position = cgpointmake(-20, 0) background.size = cgsizemake(self.size.width + 40, self.size.height) background.anchorpoint = cgpointzero background.blendmode = skblendmode.replace self.addchild(background) // add game title gametitle = skspritenode(imagenamed: "title") gametitle.name = "game title" gametitle.xscale = scale gametitle.yscale = scale gametitle.position = cgpoint(x: cgrectgetmidx(self.frame), y: self.frame.height * 0.75) self.addchild(gametitle) // add scoreboard scoreboard = skspritenode(imagenamed: "scoreboard") scoreboard.name = "scoreboard" scoreboard.xscale = scale scoreboard.yscale = scale scoreboard.position = cgpoint(x: cgrectgetmidx(self.frame), y: self.frame.height * 0.50) self.addchild(scoreboard) // add play button playbutton = skspritenode(imagenamed: "playbutton") playbutton.name = "playbutton" playbutton.xscale = scale playbutton.yscale = scale playbutton.position = cgpoint(x: cgrectgetmidx(self.frame), y: self.frame.height * 0.25) self.addchild(playbutton) // add menu score label var menuscorelabel = sklabelnode() menuscorelabel.fontname = fontname menuscorelabel.fontsize = scorefontsize menuscorelabel.text = string(score) menuscorelabel.position = cgpointmake(scoreboard.position.x - (scoreboard.size.width / 4), scoreboard.position.y - (scoreboard.size.height / 4)) menuscorelabel.zposition = 10 self.addchild(menuscorelabel) // add menu top score label var menutopscorelabel = sklabelnode() menutopscorelabel.fontname = fontname menutopscorelabel.fontsize = scorefontsize menutopscorelabel.text = string(userdefaults.integerforkey("topscore")) menutopscorelabel.position = cgpointmake(scoreboard.position.x + (scoreboard.size.width / 4), scoreboard.position.y - (scoreboard.size.height / 4)) menutopscorelabel.zposition = 10 self.addchild(menutopscorelabel) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { /* called when touch begins */ touch in (touches as! set<uitouch>) { // touch location var touchlocation = touch.locationinnode(self) // check if play button presssed if playbutton.containspoint(touchlocation) == true { //println("right node touched") // transition gamescene.swift var transition: sktransition = sktransition.fadewithduration(1) // configure view. let scene = gamescene() let skview = self.view! skview skview.showsfps = true skview.showsnodecount = true /* sprite kit applies additional optimizations improve rendering performance */ skview.ignoressiblingorder = true /* set scale mode scale fit window */ scene.size = skview.bounds.size scene.scalemode = .aspectfill skview.presentscene(scene, transition: transition) } } }
}
scene 2:
class gamescene: skscene, skphysicscontactdelegate {
override func didmovetoview(view: skview) { /* setup scene here */ // add background var background: skspritenode = skspritenode(imagenamed: "starfield") background.position = cgpointmake(-20, 0) background.size = cgsizemake(self.size.width + 40, self.size.height) background.anchorpoint = cgpointzero background.blendmode = skblendmode.replace self.addchild(background) // add mainlayer & labelholderlayer self.addchild(mainlayer) self.addchild(labelholderlayer) // add cannon cannon = skspritenode(imagenamed: "cannon") cannon.name = "cannon" cannon.position = cgpoint(x: cgrectgetmidx(self.frame), y: 0) self.addchild(cannon) // add score label scorelabel.name = "score label" scorelabel.fontname = fontname scorelabel.fontsize = scorefontsize scorelabel.text = string(score) scorelabel.position = cgpointmake(cgrectgetmidx(self.frame), self.frame.size.height - scorefontsize) scorelabel.zposition = 10 self.addchild(scorelabel) // add livesdisplay livesdisplay = skspritenode(imagenamed: "ammo5") livesdisplay.name = "livesdisplay" livesdisplay.position = cgpoint(x: self.size.width - livesdisplay.size.width, y: self.size.height - livesdisplay.size.height) self.addchild(livesdisplay) // settings new game newgame() } func gameisover() { // set game on flag , stop movement gameover = 1 mainlayer.speed = 0 mainlayer.paused = true // add game on label gameoverlabel.fontname = fontname gameoverlabel.fontsize = gameoverfontsize gameoverlabel.text = "game over!" gameoverlabel.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame)) gameoverlabel.zposition = 10 labelholderlayer.addchild(gameoverlabel) // set main menu top score if score > userdefaults.integerforkey("topscore") { userdefaults.setinteger(score, forkey: "topscore") userdefaults.synchronize() } // run acton sequence (wait few seconds before transitioning) runaction(skaction.sequence([skaction.waitforduration(1), skaction.runblock({ () -> void in // transition gamemenuscene var transition: sktransition = sktransition.fadewithduration(1) // configure view. let scene = gamemenuscene() let skview = self.view! skview skview.showsfps = true skview.showsnodecount = true /* sprite kit applies additional optimizations improve rendering performance */ skview.ignoressiblingorder = true /* set scale mode scale fit window */ scene.size = skview.bounds.size scene.scalemode = .aspectfill skview.presentscene(scene, transition: transition) })])) }
}
when launch app, can transition scene 1 scene 2. when game over, app transitions scene 1. when try go scene 2 again, app crashes. happens on device, on simulator, can go , forth without problems.
the error sknode being added exists. know means , error starts when try
// add mainlayer & labelholderlayer self.addchild(mainlayer) self.addchild(labelholderlayer)
but don't see why can add background no problem crash there on. why work on simulator not on device? need check in didmovetoview if nodes created?
i found out mistake making.
i instantiating nodes outside scene 2 class, making them global. therefore when went scene 1 -> scene 2 -> scene 1 there not problem going scene 2 again caused crash because nodes globally created.
solution:
moving following code within class solved problem.
var mainlayer = skspritenode() var labelholderlayer = skspritenode() var livesdisplay = skspritenode() var scorelabel = sklabelnode()
Comments
Post a Comment