1

I have a wrapper class to create a label node with some subnodes. This is the init for the class:

let label = SKLabelNode(text: word)
let background = SKShapeNode(rectOfSize: size, cornerRadius: 0.5)
let mainNode = SKSpriteNode(color: color, size: background.frame.size)
label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center
label.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Center
label.addChild(background)
label.addChild(mainNode)
mainNode.zPosition = 1
background.zPosition = 2
label.zPosition = 5
self.node = label

In my main scene I instantiate the node, set its physics body and set the gravity for the scene:

self.physicsWorld.gravity = CGVectorMake( 0, -10 );

let node = MyNode()
node.node.zPosition = 99
node.node.position.y = endOfScreenTop
  if let name = node.node.name as String!, let sprite = node.node.childNodeWithName(name) as? SKSpriteNode {
    node.node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
    node.node.physicsBody!.mass = 1.0;
    node.node.physicsBody!.angularDamping = 0.0;
    node.node.physicsBody!.linearDamping = 0.0;
    node.node.physicsBody!.friction = 0.0;
    node.node.physicsBody!.affectedByGravity = true
    node.node.physicsBody!.dynamic = true
    node.node.physicsBody!.allowsRotation = false
 }
 addChild(node.node)

I have 2 problems:

  • The label appears behind the SKSpriteNode, even though the zPosition should place it at the front
  • The SKShapeNode (that's just an insets to smooth the corners) doesn't appear at all
  • None of the nodes are affected by gravity

PS. Even changing the following to true doesn't change anything:

 skView.ignoresSiblingOrder = false

PS2. the MyNode class has these properties

var node: SKLabelNode
var speed: CGFloat = 1.0
var word: String
var color: UIColor
var kind: Kind
vale
  • 1,376
  • 11
  • 25
  • and of course I forgot to copy the last line from the init, great.... self.node = label (I've added more infos at the bottom) – vale Jul 26 '15 at 06:58

1 Answers1

1

Assuming that skView.ignoresSiblingOrder = true in the view controller (the default setting), some thoughts...

  1. When ignoresSiblingOrder is set to true, Sprite Kit ignores sibling relationships when determining the order in which to draw nodes. It does not, however, affect the parent-sibling draw order.
  2. In either mode, the parent is drawn first and then the child nodes are rendered. Normally, the children appear on top of the parent (if they're at the same location) regardless of the zPositions of the parent and children! The exception is if a child's zPosition is less than zero. In this case, the child will appear beneath its parent (even if the parent's zPosition is less than the child).
  3. It doesn't appear that you are setting node.node.name nor the name of the sprite node. If the names aren't set, the if condition will fail and the physics body won't be attached to node.node.
  4. The default settings for shapes only draws a white border around its path (in this case, a rectangle). You should see this as an white outline around the sprite node. Setting background.fillColor = SKColor.greenColor() will show the SKShapeNode filled with green.
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • Thanks a lot, everything works now. I deleted the SKSpriteNode and just kept the Shape not using background.fillColor. I really didn't think that the ignoresSiblingOrder acted like that during parent-sibling draw order. – vale Jul 26 '15 at 10:30
  • `ignoresSiblingOrder` should be `true` in the first sentence and in bullet 1. I mistakenly set it to `false`. I've updated my answer. Sorry about that. – 0x141E Jul 26 '15 at 16:30