Chapter 10: Challenge - seesaw joint by code

I’m trying to figure out what I’m doing wrong. I can pin the seesaw in the scene editor and it works fine. But by code, it doesn’t work. I add the joint in the didMove(to) method and when I run it, it looks like it’s actually pinned in the center, but its movements are erratic and after a while it simply crashes. Since it’s a temporary thing in the challenge, i can’t seem to find the solution for that step anywhere.

I tried every way of unwrapping optionals I know in hope it would change something, I tried to set the rotationSpeed and frictionTorque before adding the joint to the physicsWorld without any change in behaviour.

what happens is that the level starts and I already lost the game because the seesaw seem to be wiggling at a 45 deg angle super quickly and it seem to have thrown the cat away as well as the top block.

here is my code that I put at the end of didMove():

if let seesawBase = childNode(withName: "seesawBase") {
            if let seesaw = childNode(withName: "seesaw") {
                let seesawJoint = SKPhysicsJointPin.joint(withBodyA: seesawBase.physicsBody!, bodyB: seesaw.physicsBody!, anchor: seesawBase.position)
                physicsWorld.add(seesawJoint)
            }
        }

Thanks in advance for your help.

1 Like

I ran into the same thing and after a bit of searching on the forum, found the same question for an older version of the book:

which pointed to an older errata thread:

Though as someone in the question thread pointed out, it is interesting that enabling Pinning with a Collision Mask of 11 works, while the programatic route requires a Collision Mask that excludes PhysicsCategory.Edge.

5 Likes
2 Likes

I’m so glad I checked these forums, wasted over an hour trying to figure out what went wrong. Guess we should double check every step whether it’s in the book or not.

Thanks again!