Chapter 10 - CatNode.swift


Not sure why we are using “parent!” in the file (“CatNode.swift”):

parent!.physicsBody = SKPhysicsBody(texture: catBodyTexture, size: catBodyTexture.size()) parent!.physicsBody!.categoryBitMask = PhysicsCategory.Cat
parent!.physicsBody!.collisionBitMask = PhysicsCategory.Block | PhysicsCategory.Edge

Meanwhile in file “BedNode.swift” we are not:

physicsBody = SKPhysicsBody(rectangleOfSize: bedBodySize)
physicsBody!.dynamic = false
physicsBody!.categoryBitMask = PhysicsCategory.Bed
physicsBody!.collisionBitMask = PhysicsCategory.None

Not sure if this is because cat is being placed as reference node in “GameScene.sks”


On page 231 you have:

“Since the cat_body sprite is not a direct child of the scene, you can’t simply provide the name cat_body to childNodeWithName(_:slight_smile: and expect to get it back”

bed_Node or bed sprite is a child of the scene node.

cat_Node has a cat body and it is that cat body which will have the physics body. The rest of the cat, whiskers and eyes etc, are part of the cat_Node but will not have a physics body.

Earlier on p229 you saw how to search recursively:

enumerateChildNodesWithName("//*", usingBlock: {node, _ in
  if let customNode = node as? CustomNodeEvents {

That is why you get a reference to bed like this:
bedNode = childNodeWithName("bed") as! BedNode

and a reference to cat like this:
catNode = childNodeWithName("//cat_body") as! CatNode

because you are looking through the children of the scene.

So the parent! of your cat physics body, is the cat_Node.

Is that better or worse? :confused:

It is definitely better, many thanks!
All clear now.