Cat Nap Hook Problems

so I have tried everything to fix this!

func didMoveToScene() {
    guard let scene = scene else {
        return
    }
}

let ceilingFix = SKPhysicsJointFixed.joint(withBodyA:
    scene.physicsBody!, bodyB: physicsBody!, anchor: CGPoint.zero)
scene.physicsWorld.add(ceilingFix)

the lines there in chapter 10 pages 256, 257 its saying add all these lines in, but every time I try to test it it comes up with saying that the ‘didMoveToScene’ the scene was defined but never used, and the ‘ceilingFix’ always comes up with exc_bad_instruction (code=exc_i386_invop subcode=0x0), I can’t do anything to fix it, I thought maybe I stuffed something up back in a previous chapter, but I even tried it on the chapter 10 starter code which is provided and I keep getting the same errors?? PLEASE HELP

It looks like you have a misplaced curly brace. the “let ceilingFix…” should be inside of the didMoveToScene() function. Try that and see if it helps with the crash.

It’s been a while since I’ve gone through the tutorial myself, but I’m guessing the unused didMoveToScene warning is related to the custom protocol that is added. Double check you’ve done everything with the CustomNodeEvents

I tried putting it all in the didMoveToScene() that gets rid of the first error, checked all customNodes all are looking good from my point of view but still getting the exc_bad_instruction error and when I play it, it crashes and says fatal error unexpectedly found nil while unwrapping an Optional value

So either 1) scene.physicsBody is nil, or 2) self.physicsBody is nil.

  1. In your SKScene, make sure you are setting the physicsBody to the edgeLoop in didMoveToView(view).
    physicsBody = SKPhysicsBody(edgeLoopFromRect: playableRect)

  2. Make sure hook node is a subclass of SKSpriteNode, and not SKNode (I don’t believe SKNodes have physics bodies)

Once you know which physicsBody is causing the crash, it will make it easier to debug.

Performing Chapter 10 tutorial of 2D Apple Games by Tutorial 2.0 page 257, added to HookBaseNode.swift didMoveToScene():

CatNap crashes immediately with console message “fatal error: unexpectedly found nil while unwrapping an Optional value” and:

I found that if I enabled the above guard statement that CatNap would call this routine twice. The first time through the guard statement returns because bodyB: physicsBody is nil. The second time through the guard statement allows execution to proceed, and CatNap proceeds normally with Level2.

I could find no difference in my HookBaseNode.swift, Level2.sks, or any other source member, from the Chapter 10 Final project source members.

I eventually determined that if I replaced Level2.sks in my project with Level2.sks from the Chapter 10 Final project, that my project would work normally. (I also did the reverse, and my Level2.sks causes the Chapter 10 Final project to get the same “fatal error…”.

I tried remaking my Level2.sks file from scratch, following the tutorial again.

I’ve compared the properties of all the nodes in my Level2.sks file with those in Chapter 10 Final’s, and could find no differences.

The only difference that I could ascertain between my Level2.sks file and the one in the Chapter 10 Final project is that my file is 12k in file size, whereas Final’s is 11k.

Any ideas?

101airborne, the problem may be in your Level2.sks file. Specifically:

  1. Choose Level2.sks
  2. Select the hookBase object
  3. Look in the Property Inspector. In Physics Definition, the Body Type must be “Bounding rectangle”. If it is “None” (the default), you’ll get the error you’ve seen about unwrapping a nil. I’ve been able to reproduce this problem; setting it to “Bounding rectangle” solves the problem.

Thanks orangephoenix for your reply. Unfortunately, in both my Level2.sks and the one from Chapter 10 Final’s, node hookBase’s Physics Definition/Body Type is set to Bounding rectangle. I tried setting it to Alpha mask or Bounding circle, and get the same failure.

Since I can find no reason accounting for the difference in Level2.sks file sizes mentioned in my post, I’m beginning to wonder if my Level2.sks is somehow malformed by Xcode from the get-go.

My development environment is OS X 10.12.3 and Xcode 8.2.1.

I am still having the same problem, I even thought my coding skills weren’t good so a read through and finished IOS APPRENTICE, and then even decided to start 2D games book from scratch again and I get to page 257, and bang FATAL Error, i still don’t know whats going on

Has there been a solution for this error? I’ve tried over and over starting this section from the beginning. I thought I could set the dynamic to false and it was fine but then I got to the hook and it asks for the spring joint and now I am experiencing the same problem. :frowning:

I had the same issue and the problem in my Level2.sks was that the SKSpriteNode for the background was having the custom class of HookBaseNode.
I am not sure why but I am sure I did not assign this myself.

In any case you should check that all sprites have the right custom class in place. (in my case I cleared the custom class)

Hope this helps!

2 Likes

If your Level2 doesn’t work and the one from the book does, the problem is in your file and what mierea is suggesting might be the cause.

I’ve noticed this bug for a while now. For what I’ve experienced, it happens when some of the nodes in the scene don’t have a name.
In Level2, if you drag a Color Sprite to the scene, name it, go to the Custom Class inspector and then go right back to the Attributes Inspector, you’ll notice that you now have the BACKGROUND node selected instead of the one you SHOULD have selected, which means that if you assigned a Custom Class, you were actually assigning it to the ‘background’ node.
If (in Level2) you name the ‘background’ node and do the same thing, now the new node instead of being assigned to ‘background’ it will be assigned to (the also unnamed node) ‘wood_horiz1’ used as the base of the hook.

If all the nodes have a name (like in Level6) and do the same steps, now you will end up with no selection at all when you switch from the Attributes inspector to the Custom Class inspector.

So now I either type the Custom Class first and then name the node or I deselect the node after naming it and then select it again and triple check that everything is ok because if can cause you a big headache otherwise.

Also, if you type a name or a Custom Class and click directly on either Attributes inspector or Custom Class inspector without first either pressing TAB or ENTER, they WON’T get saved.
So these are a couple of little (and very annoying) things to have in mind :stuck_out_tongue:

1 Like

I had a problem like this and traced it to the hookBaseNode returning Nil when Level2 loads. After much searching I realised that in the sks file when I added the hookBase I had given it the name hook_base but when loading it up in the GameScene in didMove(to:) I called it hookBase. A simple mistake but it took some time to figure out.