ios - Xcode keeps returning "fatal error: unexpectedly found nil while unwrapping an Optional value", when attempting to add an impulse -
i trying add impulse ball reffered in code, xcode keeps returning "fatal error: unexpectedly found nil while unwrapping optional value"
class gamescene: skscene { override func didmovetoview(view: skview) { super.didmovetoview(view) let borderbody = skphysicsbody(edgeloopfromrect: self.frame) borderbody.friction = 0 self.physicsbody = borderbody physicsworld.gravity = cgvectormake(0, 0) let ball = childnodewithname(ballcategoryname) skspritenode ball.physicsbody!.applyimpulse(cgvectormake(10, -10)) } }
i know late, have real solution! using this tutorial, i'm assuming. came across question while searching same issue.
so xcode throwing error because ball you're referencing nil. reason because line:
let ball = childnodewithname(ballcategoryname) skspritenode
is referencing sprite .sks file name of ballcategoryname
or "ball"
. funny thing is: tutorial never told set name. so, can't find sprite named "ball"
. go , name ball sprite ball
, code work!!