Centre a SKLabelNode on a SKSpriteNode

后端 未结 3 1149
半阙折子戏
半阙折子戏 2021-02-19 20:18

I have an SKLabelNode that is the child of a SKSpriteNode because I\'m trying to create a Button class to create buttons in an easier way. I\'ve tried a couple of things using t

3条回答
  •  有刺的猬
    2021-02-19 20:57

    I realized how to solve this...here's what i did. Keep in mind that I have a class called Button that is a subclass of SKSpriteNode.

    In the Button.m class I have an instance variable called label that is a SKLabelNode. I add the label node as a child to the button then set the horizontal and vertical alignment modes to centre.

    label = [[SKLabelNode alloc] init];
    [self addChild:label];
    [label setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeCenter];
    [label setVerticalAlignmentMode:SKLabelVerticalAlignmentModeCenter];
    

提交回复
热议问题