0
-(void)drawGUIForPlayer:(PlayerClass *)player {

    SKShapeNode *outlineBox = [SKShapeNode shapeNodeWithRect:CGRectMake(0, self.view.frame.size.height - 60, 150, 30)];
    outlineBox.fillColor = [SKColor colorWithRed:0 green:0 blue:1 alpha:0.3];
    outlineBox.strokeColor = [SKColor clearColor];
    [self addChild:outlineBox];

    SKLabelNode *playerName = [SKLabelNode labelNodeWithText:player.name];
    playerName.color = [SKColor colorWithWhite:1 alpha:0.5];
    playerName.fontSize = 10;
    [outlineBox addChild:playerName];
    [playerName setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeCenter];
    [playerName setVerticalAlignmentMode:SKLabelVerticalAlignmentModeCenter];

}

Previously, I would create such a technique to say add a box, then put the label as child of the box. Essentially making the box a view so I could position the label relative to the shapenode. However, with this I'm getting a position of 0,0 which positions it relative to the self.view as opposed to the outlineBox.view.

Can anyone point me in the right direction?

Daniel
  • 285
  • 3
  • 12

1 Answers1

1

I think you perhaps misunderstand what [SKShapeNode shapeNodeWithRect:CGRectMake(0, self.view.frame.size.height - 60, 150, 30)] is doing.

As per Apple Documentation https://developer.apple.com/reference/spritekit/skshapenode/1520047-shapenodewithrect?language=objc:

A rectangle, relative to the node’s origin.

Your outlineBox is not "centered" it's parent.

outlineBox's parent is self. playerName's parent is outlineBox. As you have not adjusted any transforms on outlineBox it will appear as if it is relative to self.

Note you should not be referring to SKNode as having a view. They do not.

Mobile Ben
  • 7,121
  • 1
  • 27
  • 43
  • Ah, thank you for the clear explanation. I know it's not a view. However, you can position it's children `as though` it's a view. And that's usually my practice. Just a simple position addition has fixed the issue. Thank you. – Daniel Sep 26 '16 at 16:56
  • @Daniel cool, glad it worked out. The only reason I mentioned that was so it would be less "confusing" to folks reading it. BTW, if this answer worked out, you should tag it as such so people will know it is as such so they won't spend time on it. – Mobile Ben Sep 26 '16 at 17:00