I want to make a text-based game using Sprite Kit (a la those Learn to Type games).
I thought I'd use SKLabelNode for strings, but when I try to set the anchorPoint in order to rotate it, I get an error that SKLabelNode doesn't have the anchorPoint property:
SKLabelNode *hello = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"];
hello.text = @"Hello,";
//this throws an error:
hello.anchorPoint = CGPointMake(0.5,1.0);
What's a good workaround? How can I vertically orient my text strings, while treating them like physical objects using physicsBody?
You can add the SKLabelNode as a child of a SKSpriteNode. Then apply the anchorPoint (and rotation etc) to the parent node:
- (SKSpriteNode *)testNode {
SKSpriteNode *testNode = [[SKSpriteNode alloc] init];//parent
SKLabelNode *hello = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"];//child
hello.text = @"Hello,";
[testNode addChild:hello];
testNode.anchorPoint = CGPointMake(0.5,1.0);
testNode.position=CGPointMake(self.frame.size.width/2,self.frame.size.height/2);
return testNode;
}
SKLabelNode doesn't have anchor point.
Use verticalAlignmentMode
property to align SKLabelNode vertically.
SKLabelVerticalAlignmentModeBaseline
Positions the text so that the font’s baseline lies on the node’s origin.
SKLabelVerticalAlignmentModeCenter
Centers the text vertically on the node’s origin.
SKLabelVerticalAlignmentModeTop
Positions the text so that the top of the text is on the node’s origin.
SKLabelVerticalAlignmentModeBottom
Positions the text so that the bottom of the text is on the node’s origin.
https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKLabelNode_Ref/Reference/Reference.html#//apple_ref/doc/uid/TP40013022-CH1-SW15
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With