Group Group Group Group Group Group Group Group Group

Stuck on Cat Nap chapter 9

Hi, I finally made it to chapter 9 (intermediate physics) however after entering code on lines 38-40 I’m get getting an errors: ”= Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value” I also tried running the final source code for chapter 10 and the same error appears. Prior to enter those lines of code, I can destroy the blocks but the cat won’t fall. I’m a beginners to programming and having a hard trying to figure this out. Thanks for any solutions.

@ben_eaton_goode Do you still have issues with this?

I did the following to get it to work:

Within CatNode.swift I added the following (points could use some fine-tuning):
var splinePoints = [CGPoint(x: -80, y: -75),
CGPoint(x: -90, y: -25),
CGPoint(x: -20, y: 180),
CGPoint(x: -90, y: 280),
CGPoint(x: -20, y: 300),
CGPoint(x: 80, y: 300),
CGPoint(x: 175, y: 250),
CGPoint(x: 100, y: 200),
CGPoint(x: 135, y: 75),
CGPoint(x: 120, y: -75),
CGPoint(x: -80, y: -75)]

    let catRoughShape = SKShapeNode(splinePoints: &splinePoints,
                             count: splinePoints.count)
    catRoughShape.lineWidth = 2
    self.physicsBody = SKPhysicsBody(polygonFrom: catRoughShape.path!)
    self.physicsBody!.affectedByGravity = true
    self.physicsBody!.isDynamic = true

As an update to the above post, the y-values need to be adjusted and the physics body should be to parent instead of self to be more consistent with the tutorial.

I just entered your suggested code and the cat fell (The cat has gravity). Thanks a million. Do you have any suggestions on learning SpriteKit for newbie programmers?

No, credit to Jeffbls who gave code that gave the cat gravity. Now onward with rest of the book.

Honestly, I usually use the tutorials from this website. They are pretty good most of the time. Looking at other forums, it looks like an update by Apple broke this tutorial (as well as hundreds of other people’s code). It’s an unfortunate side effect of working with a tool kit that is being actively developed.

@jeffbls Thank you for sharing your solution - much appreciated!