Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't vertically orient a SKLabelNode with anchorPoint

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?

like image 569
Nick Barr Avatar asked Sep 30 '13 21:09

Nick Barr


2 Answers

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;
}
like image 168
AndyOS Avatar answered Oct 19 '22 14:10

AndyOS


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

like image 43
User1234 Avatar answered Oct 19 '22 13:10

User1234