2013-11-01 48 views
2

SKActioncolorizeWithColor:根據文檔只做與SKSpriteNode一起使用,那麼我們怎樣處理SKLabelNode? SKLabelNode 確實同時具有可以靜態設置的colorcolorBlendFactor屬性。有沒有什麼方法可以用SKAction來動畫?colorizeWithColor和SKLabelNode

我目前的做法是呈現SKLabelNode使用SKView的實例方法textureFromNode紋理,但只得到零質地指出,ATM的:-(

更新:你怎麼知道,我想我找到了在SKScene的init方法中無法修改紋理,因爲self.view在這個點上是零,所以我在didMoveToView中嘗試了它,並且紋理渲染了,不管怎樣:-)

回答

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!"); 
} 
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]; 
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方法工作正常,但字體嚴重損壞且粗糙。所以,沒用。

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

+0

我重新使用了它,非常感謝。 –

+1

我很高興你發現它很有用。其實我找到了一個更好的解決方案。我在標籤更改時創建SKTexture,然後從該紋理創建sprite節點,這允許我在這個新的spritenode上使用colorizeWithColor操作,並在其上添加文本。我正在展示spritenode而不是labelnode。此方法佔用較少的cpu。 – GuessGen