Reputation: 9064
Why does my SKLabelNode always draw behind my SKSpriteNode even though I'm adding it after the sprite?
_startButton = [SKSpriteNode spriteNodeWithImageNamed:@"[email protected]"];
_startButton.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/2);
_startButton.name = @"start";
_startButton.zPosition = 1.0;
[self addChild:_startButton];
SKLabelNode *start = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
start.text = @"Start";
start.fontSize = 40;
start.fontColor = [SKColor whiteColor];
start.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:start];
Upvotes: 0
Views: 1128
Reputation: 26223
I am pretty sure that the default zPosition is 0 which means that your sprite (with a zPosition of 1) will appear in front of the label. Try setting the SKLabelNode
zPosition to 2. Larger positive numbers place items on top of those with smaller numbers.
The default value is 0.0. The positive z axis is projected toward the viewer so that nodes with larger z values are closer to the viewer. When a node tree is rendered, the height of each node (in absolute coordinates) is calculated and then all nodes in the tree are rendered from smallest z value to largest z value. If multiple nodes share the same z position, those nodes are sorted so that parent nodes are drawn before their children, and siblings are rendered in the order that they appear in their parent’s children array. Hit-testing is processed in the opposite order.
Upvotes: 4