I have a player node in Player.sks. In GameScene.sks I create a reference on the player node. Player contains from four parts: body, lanternArm (child of body), lantern (child of lanternArm), light node (child of lantern). I'd like to create compound node (body and lanternArm). I know how to do this if player placed on the GameScene.sks without referencing, but I can't do this if player is reference node. If player placed on the scene without referencing, I do in PlayerNode class:
func didMoveToScene() {
guard let scene = scene else {
return
}
scene.addChild(PlayerNode.makeCompoundNode(in: scene))
}
static func makeCompoundNode(in scene: SKScene) -> SKNode {
let compound = PlayerNode()
for part in scene.children.filter( { node in node is PlayerNode } ) {
part.removeFromParent()
compound.addChild(part)
}
let bodies = compound.children.map( { node in SKPhysicsBody(rectangleOf: node.frame.size, center: node.position) } )
compound.physicsBody = SKPhysicsBody(bodies: bodies)
compound.physicsBody!.categoryBitMask = PhysicsCategory.Player
compound.physicsBody!.collisionBitMask = PhysicsCategory.Zombie | PhysicsCategory.Wall
compound.zPosition = 50
return compound
}