我在Player.sks中有一个播放器节点。在GameScene.sks中,我在播放器节点上创建了一个引用。玩家包含四个部分:身体,灯笼(身体的孩子),灯笼(灯笼的孩子),灯节点(灯笼的孩子)。 我想创建复合节点(body和lanternAm)。我知道如果玩家在没有参考的情况下放置在GameScene.sks上,如何做到这一点,但如果玩家是参考节点,我就不能这样做。 如果玩家在没有引用的情况下放置在场景中,我会在PlayerNode类中执行:
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
}