ios - unexpectedly found nil while unwrapping an Optional value - Sprite Kit -


hey guys im having huge problem sprite kit game. im new , im trying kind of asteroids game exercise learnt.

my problem is, asteroids infinite, don't stop unless player hit. code follows:

for meteor:

  func addmeteor() {          let bigmeteor = skspritenode(imagenamed: "meteor1")          let bigmeteoraux = bigmeteor         let sizex = uint32(cgrectgetmaxx(self.frame))         let randomx = cgfloat(arc4random_uniform(sizex))         var impulse : cgvector!          bigmeteoraux.position = cgpoint(x: randomx, y: size.height + 100)         impulse = cgvector(dx: 0, dy: -30)         bigmeteoraux.physicsbody = skphysicsbody(texture: bigmeteor.texture, size: bigmeteor.size);         bigmeteor.physicsbody?.affectedbygravity = false         bigmeteor.physicsbody?.allowsrotation = false         bigmeteoraux.physicsbody?.friction = 0.0         bigmeteoraux.physicsbody!.categorybitmask = collisioncategoryasteroids         bigmeteoraux.name = "asteroid"          foregroundnode!.addchild(bigmeteoraux)          bigmeteoraux.physicsbody!.applyimpulse(impulse)      } 

im calling function action:

   runaction(skaction.repeatactionforever(         skaction.sequence([             skaction.runblock(addmeteor),             skaction.waitforduration(1.0)             ])         )) 

perfect until now, game works fine. player code:

    playernode = skspritenode(imagenamed: "spaceshipv2")     playernode!.physicsbody = skphysicsbody(texture: playernode!.texture, size: playernode!.size);     playernode!.xscale = 0.5     playernode!.yscale = 0.5     playernode!.position = cgpoint(x: size.width / 2.0, y: 220.0)     playernode!.physicsbody!.lineardamping = 1.0     playernode!.physicsbody!.allowsrotation = false     playernode!.physicsbody?.affectedbygravity = false     playernode!.physicsbody!.categorybitmask = collisioncategoryplayer     playernode!.physicsbody!.contacttestbitmask =         collisioncategoryasteroids     playernode!.name = "player"     //addchild(playernode!)     foregroundnode!.addchild(playernode!) 

and last contact func:

    func didbegincontact(contact: skphysicscontact) {          var nodeb = contact.bodyb!.node!          if nodeb.name == "asteroid"  {              println("touched")             nodeb.removefromparent()         }      } 

so problem start nodeb, reason when asteroid hit player code works fine other times when asteroid hit player game crashes ,

fatal error: unexpectedly found nil while unwrapping optional value

after program enters contact function.

any ideas or solution how fix this? thx lot! =)

the crash comes 1 of 2 forced unwrap calls: bodyb! , node!. writing this, asserting sure never nil.

the way find case 1 of these nil, break code lines can check breakpoint.

var bodyb = contact.bodyb if bodyb == nil {     // breakpoint here } var nodeb = bodyb!.node if nodeb == nil {     // breakpoint here } if nodeb!.name = "asteroid" // etc. 

once code stops can examine objects , try find out why nil , fix problem.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -