1

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
}
ThePyzhov
  • 259
  • 2
  • 14
  • Code would be exactly the same with the exception of your `let compound = PlayerNode()`, you just do `let compound = scene.childNode(withName:"whateverplayernodenameis")` – Knight0fDragon May 18 '17 at 13:10
  • you may need to grab the child, I do not remember the exact hierarchy of a reference – Knight0fDragon May 18 '17 at 13:11
  • If I initialise `compound` as a `scene.childNode(withName: "//body")` I catch an error: terminating with uncaught exception of type NSException. I think it's because `scene` in `didMoveToScene()` is a `GameScene`, and `GameScene` contains a reference on `Player.sks`. – ThePyzhov May 18 '17 at 15:47
  • no that woouldnt matter, it can't find the reference called "//body", I would think that is the error – Knight0fDragon May 18 '17 at 16:27
  • Reference name is "player_shared". Both nodes ("body" and "player_shared") are found. `if let compound = scene.childNode(withName: "//player_shared") { print("found!") }` Error isn't here. – ThePyzhov May 18 '17 at 16:41
  • I found smth strange. I can't add any nodes to the `scene`, but I can add to the `parent!`. – ThePyzhov May 18 '17 at 18:33
  • I dunno man, you need to hit up some tutorials and see if what you are doing is even right – Knight0fDragon May 18 '17 at 18:34
  • oh, btw, do not pass scene around unless you absolutely have to, every node knows what scene it is on, you should not be playing guessing games – Knight0fDragon May 18 '17 at 18:35
  • see my post http://stackoverflow.com/questions/39390375/add-skreferencenode-skscene-to-another-skscene-in-spritekit/39396685#39396685 – Simone Pistecchia May 19 '17 at 14:12

0 Answers0