Unable to vertically orient SKLabelNode with anchorPoint

I want to create a text game using the Sprite Kit (a la the Learn to Type games).

I thought I would use SKLabelNode for strings, but when I try to set anchorPoint to rotate it, I get an error that SKLabelNode does not have anchorPoint property:

SKLabelNode *hello = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"]; hello.text = @"Hello,"; //this throws an error: hello.anchorPoint = CGPointMake(0.5,1.0); 

What is a good workaround? How can I vertically orient my text strings, treating them as physical objects using the physicalBody function?

+6
source share
4 answers

In the end, I realized that I didn't need anchorPoint at all for what I was trying to achieve - instead, I used

 hello.zRotation = M_PI/2; 

This works for SKLabelNode.

+1
source

SKLabelNode does not have a nodal point.

Use the verticalAlignmentMode property to align SKLabelNode vertically.

 SKLabelVerticalAlignmentModeBaseline 

Positions the text so that the font source line is on the source node.

 SKLabelVerticalAlignmentModeCenter 

Center the text vertically at the beginning of the node.

 SKLabelVerticalAlignmentModeTop 

Positions the text so that the top of the text is at the beginning of the node.

 SKLabelVerticalAlignmentModeBottom 

Positions the text so that the bottom of the text is at the beginning of the node.

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKLabelNode_Ref/Reference/Reference.html#//apple_ref/doc/uid/TP40013022-CH1-SW15

+10
source

Here is how I could solve your problem

  SKLabelNode *labNode = [SKLabelNode labelNodeWithFontNamed:@"Arial"]; labNode.fontSize = 30.0f; labNode.fontColor = [SKColor yellowColor]; labNode.text = @"TEST"; SKTexture *texture; SKView *textureView = [SKView new]; texture = [textureView textureFromNode:labNode]; texture.filteringMode = SKTextureFilteringNearest; SKSpriteNode *spriteText = [SKSpriteNode spriteNodeWithTexture:texture]; //spriteText.position = put me someplace good; [self addChild:spriteText]; 
+2
source

You can add SKLabelNode as a child of SKSpriteNode. Then apply 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; } 
+1
source

Source: https://habr.com/ru/post/954978/


All Articles