Sprite Kit: can not see existing node's children

96 Views Asked by At

I'm building a game with SpriteKit. This is my node tree: self.world.hydraAnchor.head

The problem is that head can not be seen

The code is simple

let world = SKSpriteNode()
addChild(world)
...
let hydraAnchor = SKSpriteNode()
hydraAnchor.position = anchorsPosition
world.addChild(hydraAnchor)

let head = SKSpriteNode(imageNamed: "hydraHead")
head.position = CGPointMake(anchorsPosition.x, anchorsPosition.y + 50)
hydraAnchor.addChild(head)

I can not see head when building the project, while println(hydraAnchor.children) gives [<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'hydraHead' (50 x 50)] position:{700.50396728515625, 2486.333251953125} size:{25, 25} rotation:0.00]

While when instead of adding head to hydraAnchor I do this:

world.addChild(head)

head can be seen.

zPosition, invisible and alpha properties are not related to the problem.

2

There are 2 best solutions below

0
On BEST ANSWER

You are forgetting that the coordinate system of a node's child is different than that of the parent. Your parent node's "worldNode" position might be at x:500, y:500 and adding a child 20 points on top of the parent is going to x:0 y:20.

You are adding the child by using worldNode coordinates and not parent coordinates.

0
On

When head is a child of hydraAnchor, the position will be relative to hydraAnchor. You are probably looking for something more like this:

let world = SKSpriteNode()
addChild.world()
...
let hydraAnchor = SKSpriteNode()
hydraAnchor.position = anchorsPosition
world.addChild(hydraAnchor)

let head = SKSpriteNode(imageNamed: "hydraHead")
// Position relative to hydraAnchor
head.position = CGPointMake(anchorsPosition.size.width / 2, anchorsPosition.size.height + head.size.height / 2)
hydraAnchor.addChild(head)