2

Near as I can tell, there isn’t a way to add physics joints in the scene editor. Is that right?

Consider a simple person object with a body, child legs and arms w/ pin joints. If I want to design this person in the scene editor and then programmatically add him to a scene, I’m not getting very far. I’m able to find the nodes in the scene, remove them from their parent and add them as a child at a new position in my scene, but I still have to specify all their joints manually.

Thoughts?

Dennis
  • 555
  • 8
  • 17

1 Answers1

1

Here is my solution. I'm still hoping there is a way to create physics joints with the scene editor but I haven't found it so...

Step 1) Add all the child nodes to the scene, ensuring that objects are grouped by parent.

Parent Node Location

Step 2) Define a swift class for your complex node.

class MyNode : SKSpriteNode {     
    func spawn(parentNode: SKNode, position: CGPoint) {
      parentNode.addChild(self) // before physics joints
      let arm = self.childNode(withName:"arm")
      // note if you didn't add physics bodies in scene file
      // do that first
      let shoulders = SKPhysicsJointPin.joint(withBodyA:self.physicsBody!, bodyB:   arm.physicsBody!, anchor: CGPoint(x:position.x,y:position.y-1))

      scene!.physicsWorld.add(shoulders)

      // feature of pulling a child from a scene, it's always paused by default.
      self.isPaused = false
    }
}

Set the class for your body node in your scene.

Custom Class

Step 3) Transfer your node to your game scene at init time.

let tmpScene = SKScene.init(fileNamed: "MyNode.sks")
var myNode = tmpScene.childNamed(withName:"myNode") as! MyNode
myNode.removeFromParent()
myNode.spawn(world, position) // or your own parent and position as needed for your scene
Dennis
  • 555
  • 8
  • 17