首先了解一个SKSpriteNode的anchorPoint
1.一个SKLableNode 加在一个SKSpriteNode的时候效果如下:
SKSpriteNode *bgNode = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(260, 70)];
bgNode.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:bgNode];
SKLabelNode *lab = [SKLabelNode labelNodeWithText:@"lable text did finish"];
[bgNode addChild:lab];
其中有几个默认的隐藏:
bgNode.anchorPoint = CGPointMake(0.5, 0.5);//
lab.position = CGPointMake(0, 0); //
lab.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;//
lab.verticalAlignmentMode = SKLabelVerticalAlignmentModeBaseline;//
可以看出lab是以父节点bgNode的中心渲染的。可以将lab的verticalAlignmentMode更改为中心
lab.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;//
2.如果将父节点bgNode的anchorPoint改为(0,0)
SKSpriteNode *bgNode = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(260, 70)];
bgNode.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:bgNode];
bgNode.anchorPoint = CGPointMake(0, 0);//
SKLabelNode *lab = [SKLabelNode labelNodeWithText:@"lable text did finish"];
[bgNode addChild:lab];
lab.position = CGPointMake(0, 0); //
lab.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;//
lab.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;//
可以清楚的看到 lab的中心点的位置
总结:SKLabelNode的默认postition是 (0,0),它是以父节点的anchorPoint所在位置渲染的。父节点有9个anchorPoint。所以一般来说不用去更改SKLabelNode的position,去更改父节点的anchorPoint就可以达到理想的效果。
3.比如想要左对齐,上下居中:
SKSpriteNode *bgNode = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(260, 70)];
bgNode.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:bgNode];
bgNode.anchorPoint = CGPointMake(0, 0.5);//
SKLabelNode *lab = [SKLabelNode labelNodeWithText:@"lable text did finish"];
[bgNode addChild:lab];
lab.position = CGPointMake(0, 0); //
lab.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;//
lab.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;//