ios - Swift: Fatal error: unexpectedly found nil while unwrapping an Optional value (SpriteKit) -
i've started learning program ios
games spritekit
, i'm novice programming (i have programmed in java , swift before this). started out doing tutorial found online. i'm @ point trying add "game over" scene, , keep getting error
"thread 1:exc-bad_instruction(code=exc_i386_invop,subcode=0x0)"
when declaring gameoverlabel
constant. compiles crashes @ run time ball hits bottom of screen, supposed trigger 'game over' screen.
import spritekit let gameoverlabelcategoryname = "gameoverlabel" class gameoverscene: skscene { var gamewon: bool = false { didset { let gameoverlabel = childnodewithname(gameoverlabelcategoryname) as! sklabelnode gameoverlabel.text = gamewon ? "game won" : "game over" } } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { if let view = view { let gamescene = gamescene.unarchivefromfile("gamescene") as! gamescene view.presentscene(gamescene) } } }
also if has suggestions of places can go learn more spritekit
, articles or videos, appreciated things have found have been in objective-c , doing in swift.
here tutorial have been following..
childnodewithname return nil if node name not exist. code not checking possibility (the as! assumes both not nil , of appropriate type) causing crash.
the tutorial asks create 'gameoverlabel' node , name before creating code. check did correctly , did not misname (it case sensitive example).
Comments
Post a Comment