SKAction.colorizeWithColor使SKLabelNode消失 [英] SKAction.colorizeWithColor makes SKLabelNode disappear

查看:146
本文介绍了SKAction.colorizeWithColor使SKLabelNode消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SKLabelNode.我正在创建它,并将其作为一个孩子添加到我的场景中,并且显示没有问题,但是当我尝试使用colorizeWithColor()方法更改它的颜色(不是fontColor)时,标签逐渐淡出.

I'm using SKLabelNode. I'm creating it and add it to my scene as a child and it displays with no problem, but when I try to change it's color (not fontColor) with the colorizeWithColor() method the label fades out.

问题所在:

myLabel.runAction(SKAction.colorizeWithColor(SKColor.blueColor(), colorBlendFactor: 1.0, duration: duration))

完成此操作后,我将myLabel.color属性打印到控制台,这是我得到的:

I printed to the console the myLabel.color property after the completion of this action and here is what I get:

Optional(UIDeviceRGBColorSpace 0.99178 0.99178 1 0.00822043)

您可以看到,alpha值几乎为0,所以我想这就是标签消失的原因,但是我不明白为什么会这样.

As you can see, the alpha value is almost 0, so I guess this is why the label disappears, but I don't understand why this is happening.

谢谢.

更新: 好的,所以我实际上发现了这个问题,这是我问之前没有搜索过的问题.这是有关colorizeWithColor方法的文档:

UPDATE: Ok, so I found actually this and is my bad that I didn't searched before asking. Here is the documentation about colorizeWithColor method:

此动作只能由SKSpriteNode对象执行.当...的时候 动作执行后,精灵的color和colorBlendFactor属性 被赋予了新的价值.

This action can only be executed by an SKSpriteNode object. When the action executes, the sprite’s color and colorBlendFactor properties are animated to their new values.

因此,也许有人确实知道可以对不断更新的SKLabelNode进行着色的好方法吗?

So maybe anybody does know a good work around for colorize a SKLabelNode that is always updating?

!!!最后更新!!!

!!!LAST UPDATE!!!

我能够找到一个解决方案,但是0x141E提出了一个更好的解决方案,我使用它并创建了下一个方法,当您需要从颜色A过渡到颜色B时,该方法很好用. 0x141E您曾经回到fontColor,并且当您更改颜色时它会闪烁.就我而言,它更改了fontColor而不是color属性,这导致了一个非常漂亮的过渡(当然不是很好).

I was able to find a solution, but 0x141E came up with a even more nice solution, which I used and created the next method that works nice when you need to make transition from color A to color B. In solution suggested by 0x141E you ever come back to fontColor and it causes to blink when you change color. In my case it changes the fontColor, not the color property, which causes a pretty nice transition (Of course not great).

再次感谢0x141E,这是一种非常不错的方法!

Thanks again 0x141E for the really nice approach!!!

这是我的解决方案:

当您使用duration = 0.5调用参数的方法时,这种特殊情况非常有用 但是,如果您需要其他时间,则可以使用发送的withDuration参数或乘数,在我的代码中为5. 当然,甚至应该有一个更好的解决方案,因此,如果您发现它,请与我们分享.对于我的需求,这个效果很棒.

This particular case works great when you call the method for parameter withDuration = 0.5 However if you need other time, you can play around with the sent withDuration parameter or the multiplier, that in my code is 5. Even there of course should be a better solution so if you find please share it. For my needs this one works fantastic.

首先,观看视频,以便您了解它的工作原理: https://www.youtube.com/watch?v=ZIz8Bn0-hUA&feature=youtu.be

First of all a video so you can see how it works: https://www.youtube.com/watch?v=ZIz8Bn0-hUA&feature=youtu.be

  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)*5))
      let finalGreen = fromColorComponents[1] + (toColorComponents[1] - fromColorComponents[1])*CGFloat(elapsedTime / (CGFloat(withDuration)*5))
      let finalBlue = fromColorComponents[2] + (toColorComponents[2] - fromColorComponents[2])*CGFloat(elapsedTime / (CGFloat(withDuration)*5))
      let finalAlpha = fromColorComponents[3] + (toColorComponents[3] - fromColorComponents[3])*CGFloat(elapsedTime / (CGFloat(withDuration)*5))

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

推荐答案

您可以通过创建自定义操作将SKLabelNode着色为SKAction.这是一个如何做到这一点的例子

You can colorize an SKLabelNode with an SKAction by creating a custom action. Here's an example of how to do that

myLabel.color = SKColor.blueColor()
myLabel.colorBlendFactor = 0.0

let duration:NSTimeInterval = 2.0
myLabel.runAction(SKAction.customActionWithDuration(duration, actionBlock: {
    node, elapsedTime in

    let label = node as SKLabelNode
    label.colorBlendFactor = elapsedTime / CGFloat(duration);
}))

这篇关于SKAction.colorizeWithColor使SKLabelNode消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆