ios - Swift Xcode "EXC_BAD_INSTRUCTION" -
when try run game
fatal error: unexpectedly found nil while unwrapping optional value
with thread error
exc_bad_instruction (code=exc_i386_invop, subcode=0x0)"
this happens on line of code
highscorelbl.text = nsstring(format: "highscore : %i", highscore)
in gamescene
import spritekit class gamescene: skscene, skphysicscontactdelegate { var movingground: csmovingground! var hero: cshero! var cloudgenerator: cscloudgenerator! var wallgenerator: cswallgenerator! var wall: cswall! var scorelabel = uilabel() var score = int() var isstarted = false var herocategory: uint32 = 1<<1 var wallcategory: uint32 = 1<<2 var groundcategory: uint32 = 1<<2 var inviscategory: uint32 = 1<<3 let walls = cswall() var highscorelbl: uilabel! var highscore = 0 override func didmovetoview(view: skview) { backgroundcolor = uicolor(red: 159.0/255.0, green: 201.0/255, blue: 244.0/255.0, alpha: 1.0) /* let backgroundtexture = sktexture(imagenamed: "background.png") let backgroundimage = skspritenode(texture: backgroundtexture, size: view.frame.size) backgroundimage.position = view.center addchild(backgroundimage) */ var highscoredefault = nsuserdefaults.standarduserdefaults() if (highscoredefault.valueforkey("highscore") != nil){ highscore = highscoredefault.valueforkey("highscore") nsinteger! highscorelbl.text = nsstring(format: "highscore : %i", highscore) } // add ground movingground = csmovingground(size: cgsizemake(view.frame.width, kcsgroundheight)) movingground.position = cgpointmake(0, view.frame.size.height/2) self.addchild(movingground) // add hero hero = cshero() hero.position = cgpointmake(70, movingground.position.y + movingground.frame.size.height/2 + hero.frame.size.height/2) hero.physicsbody = skphysicsbody(rectangleofsize: hero.size) hero.physicsbody?.dynamic = true hero.physicsbody?.allowsrotation = false hero.physicsbody!.collisionbitmask = herocategory | wallcategory hero.physicsbody!.contacttestbitmask = wallcategory | herocategory | groundcategory self.addchild(hero) hero.breathe() // add cloud generator cloudgenerator = cscloudgenerator(color: uicolor.clearcolor(), size: view.frame.size) cloudgenerator.position = view.center addchild(cloudgenerator) cloudgenerator.populate(7) cloudgenerator.startgeneratingwithspawntime(5) // add wall generator wallgenerator = cswallgenerator(color: uicolor.clearcolor(), size: view.frame.size) wallgenerator.position = view.center wallgenerator.physicsbody = skphysicsbody(edgeloopfromrect : wallgenerator.frame) wallgenerator.physicsbody?.dynamic = true wallgenerator.physicsbody?.categorybitmask = wallcategory wallgenerator.physicsbody?.affectedbygravity = false wallgenerator.physicsbody!.collisionbitmask = 0 wallgenerator.physicsbody!.contacttestbitmask = inviscategory self.addchild(wallgenerator) let ground1 = skspritenode(color: uicolor.clearcolor(), size: cgsizemake(view.frame.size.width, 15)) ground1.position = view.center ground1.physicsbody = skphysicsbody(rectangleofsize: ground1.size) ground1.physicsbody!.dynamic = false ground1.physicsbody!.affectedbygravity = false ground1.physicsbody!.categorybitmask = groundcategory ground1.physicsbody!.collisionbitmask = groundcategory | herocategory self.addchild(ground1) let ground2 = skspritenode(color: uicolor.clearcolor(), size: cgsizemake(view.frame.size.width, 20)) ground2.position = cgpointmake(284, 98) ground2.physicsbody = skphysicsbody(rectangleofsize: ground2.size) ground2.physicsbody!.dynamic = false ground2.physicsbody!.affectedbygravity = false ground2.physicsbody!.categorybitmask = groundcategory ground2.physicsbody!.collisionbitmask = groundcategory | herocategory self.addchild(ground2) let ground3 = skspritenode(color: uicolor.clearcolor(), size: cgsizemake(20, 500)) ground3.position = cgpointmake(100, 100) ground3.physicsbody = skphysicsbody(rectangleofsize: ground3.size) ground3.physicsbody?.dynamic = true ground3.physicsbody?.affectedbygravity = false ground3.physicsbody?.categorybitmask = inviscategory ground3.physicsbody!.collisionbitmask = 0 ground3.physicsbody!.contacttestbitmask = wallcategory self.addchild(ground3) physicsworld.contactdelegate = self scorelabel = uilabel(frame: cgrect(x: 10, y: 15, width: 100, height: 20)) scorelabel.backgroundcolor = uicolor(red: 0.6, green: 0.1, blue: 0.1, alpha: 0) scorelabel.textcolor = uicolor.blackcolor() self.view?.addsubview(scorelabel) highscorelbl = uilabel(frame: cgrect(x: 10, y: 0, width: 300, height: 20)) highscorelbl.textcolor = uicolor.blackcolor() self.view?.addsubview(highscorelbl) } func start() { isstarted = true hero.stop() hero.startrunning() movingground.start() } func addscore() { score++ scorelabel.text = nsstring(format: "score : %i", score) if (score > highscore){ highscore = score highscorelbl.text = nsstring(format: "highscore : %i", highscore) var highscoredefault = nsuserdefaults.standarduserdefaults() highscoredefault.setvalue(highscore, forkey: "highscore") highscoredefault.synchronize() } } override func touchesbegan(touches: nsset, withevent event: uievent) { if !isstarted { start() score = 0 scorelabel.text = nsstring(format: "score : %i", score) wallgenerator.startgeneratingwallsevery(0.5) } else { hero.flip() } } func didbegincontact(contact: skphysicscontact) { var firstbody = skphysicsbody() var secondbody = skphysicsbody() if contact.bodya.categorybitmask < contact.bodyb.categorybitmask { firstbody = contact.bodya secondbody = contact.bodyb } else { firstbody = contact.bodyb secondbody = contact.bodya } if firstbody.categorybitmask == inviscategory || secondbody.categorybitmask == inviscategory { self.addscore() } if (firstbody.categorybitmask & uint32(herocategory)) != 0 && (secondbody.categorybitmask & uint32(wallcategory)) != 0 { self.addscore() wallgenerator.removefromparent() let reveal = sktransition.fliphorizontalwithduration(0.5) let scene = gameoverscene(size: self.size, won: false) self.view?.presentscene(scene, transition: reveal) scorelabel.removefromsuperview() } } override func update(currenttime: cftimeinterval) { /* called before each frame rendered */ } }
this line:
highscorelbl.text = nsstring(format: "highscore : %i", highscore)
implicitly unwraps highscorelbl
before initialize it, later:
highscorelbl = uilabel(frame: cgrect(x: 10, y: 0, width: 300, height: 20)) highscorelbl.textcolor = uicolor.blackcolor() self.view?.addsubview(highscorelbl)
this causes crash. must initialize variable before attempting use it.