无法使用anchorPoint垂直定向SKLabelNode

时间:2013-09-30 21:05:56

标签: objective-c rotation sprite-kit

我想使用Sprite Kit制作基于文本的游戏(那些学习打字游戏)。

我以为我会使用SKLabelNode作为字符串,但是当我尝试设置anchorPoint以便旋转它时,我得到一个错误,即SKLabelNode没有anchorPoint属性:

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

什么是好的解决方法?如何使用physicsBody将文本字符串垂直定向,同时将它们视为物理对象?

4 个答案:

答案 0 :(得分:13)

SKLabelNode没有锚点。

使用verticalAlignmentMode属性垂直对齐SKLabelNode。

SKLabelVerticalAlignmentModeBaseline

定位文本,使字体的基线位于节点的原点。

SKLabelVerticalAlignmentModeCenter

将文本垂直居中于节点的原点。

SKLabelVerticalAlignmentModeTop

定位文本,使文本的顶部位于节点的原点。

SKLabelVerticalAlignmentModeBottom

定位文本,使文本底部位于节点的原点。

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

答案 1 :(得分:2)

这就是我解决问题的方法

    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 :(得分:1)

您可以将SKLabelNode添加为SKSpriteNode的子级。然后应用anchorPoint (和旋转等)到父节点:

- (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;
}

答案 3 :(得分:1)

最后,我意识到我根本不需要anchorPoint来实现我想要实现的目标 - 相反,我使用了

hello.zRotation = M_PI/2;

这适用于SKLabelNode。