colorizeWithColor和SKLabelNode

时间:2013-11-01 01:17:19

标签: sprite-kit sklabelnode

根据文档SKAction colorizeWithColor:执行的操作仅适用于SKSpriteNode,那么我们如何处理SKLabelNode? SKLabelNode 具有可以静态设置的colorcolorBlendFactor属性。有没有办法用SKAction来制作动画?

我目前的方法是使用SKView的实例方法textureFromNode将SKLabelNode渲染到纹理,但只是从该atm获取nil纹理: - (

更新:你知道什么。我想我发现了纹理渲染的问题。在SKScene的init方法中不可能重新创建纹理,因为self.view在那时是零。所以我在didMoveToView和voila中尝试了它,纹理渲染。无论如何,谢谢: - )

4 个答案:

答案 0 :(得分:3)

SKLabelNode还有一个可以设置的fontColor属性。但是,它不响应colorizeWithColor方法。

然而,您仍然可以通过将其与另一个SKSpriteNode颜色同步来动态更改文本的颜色。如果在精灵上调用colorizeWithColor,字体颜色会随之改变。这包括指定持续时间内的颜色过渡。例如:

    [_tileCountLabel runAction:[SKAction repeatActionForever:
                            [SKAction customActionWithDuration:COLOR_TRANSITION_SPEED actionBlock:^(SKNode *node, CGFloat elapsedTime) {
    _tileCountLabel.fontColor = _tileLayer0.color;
}]]];

另外,我尝试使用SKCropNode掩码尝试通过父SKSpriteNode设置字体颜色。 SKSpriteNode上的colorizeWithColor方法有效,但字体严重损坏且粗糙。所以,没用。

答案 1 :(得分:3)

也许有人会觉得这很有用。

   func changeColorForLabelNode(labelNode: SKLabelNode, toColor: SKColor, withDuration: NSTimeInterval) {
    labelNode.runAction(SKAction.customActionWithDuration(withDuration, actionBlock: {
      node, elapsedTime in

      let label = node as SKLabelNode

      let toColorComponents = CGColorGetComponents(toColor.CGColor)
      let fromColorComponents = CGColorGetComponents(label.fontColor.CGColor)

      let finalRed = fromColorComponents[0] + (toColorComponents[0] - fromColorComponents[0])*CGFloat(elapsedTime / CGFloat(withDuration))
      let finalGreen = fromColorComponents[1] + (toColorComponents[1] - fromColorComponents[1])*CGFloat(elapsedTime / CGFloat(withDuration))
      let finalBlue = fromColorComponents[2] + (toColorComponents[2] - fromColorComponents[2])*CGFloat(elapsedTime / CGFloat(withDuration))
      let finalAlpha = fromColorComponents[3] + (toColorComponents[3] - fromColorComponents[3])*CGFloat(elapsedTime / CGFloat(withDuration))

      labelNode.fontColor = SKColor(red: finalRed, green: finalGreen, blue: finalBlue, alpha: finalAlpha)
    }))
  }

您甚至可以在此演示中查看此功能的使用结果:https://www.youtube.com/watch?v=ZIz8Bn0-hUA&feature=youtu.be

我决定在这里写一个简短的解决方案,但如果您需要更多详细信息,请查看以下问题:SKAction.colorizeWithColor makes SKLabelNode disappear

答案 2 :(得分:2)

所以这是SKTexture / SKSpriteNode解决方案。为了便于使用,它可以包装在自己的类中。要记住的一件事是在self.view不是零的地方渲染这个......

SKLabelNode *labNode = [SKLabelNode labelNodeWithFontNamed:FONT_GAME];
labNode.fontSize = 30.0f;
labNode.fontColor = [SKColor whiteColor];
labNode.text = @"TEST";

SKTexture *texture;

NSAssert(self.view != nil, @"Can't access self.view so sorry.");
texture = [self.view textureFromNode:labNode];
DLog(@"texture: %@", texture);
if (texture != nil) {
    texture.filteringMode = SKTextureFilteringNearest;

    SKSpriteNode *spriteText = [SKSpriteNode spriteNodeWithTexture:texture];
    DLog(@"spriteText.size: %@", NSStringFromSize(spriteText.size));
    spriteText.anchorPoint = ccp(0, 0.5);
    spriteText.position = ccp(0, 25);

    [self addChild:spriteText];

    [spriteText runAction:[SKAction repeatActionForever:[SKAction sequence:@[
                                                                             [SKAction colorizeWithColor:SKColor.yellowColor colorBlendFactor:1 duration:1],
                                                                             [SKAction colorizeWithColor:SKColor.yellowColor colorBlendFactor:0 duration:1],
                                                                             ]]]];
}
else {
    DLog(@"Texture is nil!");
}

答案 3 :(得分:2)

如果要为SKLabelNode着色,可以尝试将标签用作蒙版并为背景着色:

SKSpriteNode *background = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(200.0, 200.0)];
SKAction *turnRed = [SKAction colorizeWithColor:[UIColor redColor] colorBlendFactor:1.0 duration:2.0];

SKCropNode *cropNode = [SKCropNode node];
cropNode.maskNode = [yourAwesomeLabel copy];
[cropNode addChild:background];

[background runAction:turnRed];

[self addChild:cropNode];