美文网首页
SpriteKit--SKLableNode

SpriteKit--SKLableNode

作者: lionnner | 来源:发表于2017-11-13 16:09 被阅读94次

    首先了解一个SKSpriteNode的anchorPoint

    skspriteNodeAnchorpoint.png

    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];
    
    
    default.png

    其中有几个默认的隐藏:

        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;//
    
    chang1.png

    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;//
    
    chang2.png

    可以清楚的看到 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;//
    
    chang3.png

    相关文章

      网友评论

          本文标题:SpriteKit--SKLableNode

          本文链接:https://www.haomeiwen.com/subject/nhuzmxtx.html