我正在尝试使用精灵套件对精灵节点(在本例中为命名背景)进行着色,但无法更改颜色。我有一个想要着色的精灵。我正在更改精灵的颜色属性以及颜色混合因子。当我运行该应用程序时,什么也没有发生。颜色保持不变(现在是黑色)。下面是我用来更改颜色的代码:
background.anchorPoint = CGPointMake(0, .5);
background.position = position;
// ...
// this does not seems to be doing anything at all
background.color = [SKColor redColor];
background.colorBlendFactor = 1.0f;
我错过了什么吗?我通读了文档,这似乎是一个非常简单的过程。我相信我所需要的只是这两行,但它没有任何效果。有谁知道为什么这不起作用?纹理图像具有很大的透明度,这可能是导致问题的原因吗?我在不同的精灵上尝试过这个,但它从来没有起作用。
类似这样的:
bug.color = SKColorWithRGB(128, 128, 128);
bug.colorBlendFactor = 1.0f;
一定要给选择的精灵着色。问题是这是非密集混合。对于这样一个简单的任务,将 CIFilter 与 SKEffectsNode 结合使用是非常非常密集的。
我下面有一个示例,它将精灵与红色混合,使精灵变成红色,而不是原始的橙色:
- (instancetype)init
{
if (self = [super init]) {
self.physicsBody.categoryBitMask = PCFireBugCategory;
self.physicsBody.collisionBitMask = PCPlayerCategory | PCWallCategory | PCBreakableCategory | PCBoundaryCategory;
//Keeps the bugs from sliding too far when bumped
self.physicsBody.linearDamping = 1;
self.physicsBody.angularDamping = 1;
self.color = [SKColor redColor];
self.colorBlendFactor = 0.45;
}
return self;
}
我还想指出,这个类是SKSpriteNode的子类。
下面附有一个示例,其中我使用上面的确切代码应用了红色色调。
Raywenderlich 有一句话描述混合效果:
注意:因为色调颜色乘以原始颜色 如果将精灵的颜色设置为蓝色,则得到最终结果 结果不一定是蓝色的——这取决于原始的内容 彩色岛。因此,如果你想动态设置精灵的 color 为特定颜色,方便制作源颜色 白色精灵的形状(就像《僵尸康加》中的猫)。如果你想要零件 将你的精灵变成不同的颜色,你也可以分割你的精灵 分成不同的部分,每个部分都涂上不同的颜色。为了 更多信息,请参阅 Beat ‘Em Up 游戏入门套件,网址为 raywenderlich.com.
这是一个很老的问题,但我时常遇到这个问题。
colorBlendFactor
并不是人们所期望的那样,它会重新为精灵着色。事实上,与旧颜色混合的新颜色在很大程度上取决于之前该位置的颜色。
为了克服这个问题,我使用叠加精灵(如前所述)以及底层精灵和叠加精灵的 alpha 值来创建预期的颜色。
例如,要将以下精灵从蓝色慢慢混合到灰色,我使用两个精灵:
(蓝色精灵是原始精灵,灰色精灵只是蓝色精灵的一个版本,在 Mac 上使用
ColorSync Utility
进行操作。)
和这段代码(Swift):
// Load the two Textures
let TEXTURE_CLOUD_BLUE = SKTexture(imageNamed: "cloud blue")
let TEXTURE_CLOUD_GRAY = SKTexture(imageNamed: "cloud gray")
// Create an Action. Note that the second Action takes longer to finish. This makes the blend to grey slower and easier to watch
let fadeToGray = SKAction.group([
SKAction.runAction(SKAction.fadeOutWithDuration(1.0), onChildWithName: "cloudBlue"),
SKAction.runAction(SKAction.fadeInWithDuration(2.0), onChildWithName: "cloudGray"), // Slower fade in
SKAction.waitForDuration(2.0) // Actions on children return immediately
])
// Init super sprite node
super.init(texture: nil, color: UIColor.clearColor(), size: size)
// Add blue cloud
let cloudBlue = SKSpriteNode(texture: TEXTURE_CLOUD_BLUE, size: size)
cloudBlue.alpha = 1.0 // Show immediately
cloudBlue.name = "cloudBlue"
addChild(cloudBlue)
// Add grey cloud
let cloudGray = SKSpriteNode(texture: TEXTURE_CLOUD_GRAY, size: size)
cloudGray.alpha = 0.0 // Don't show at start
cloudGray.name = "cloudGray"
addChild(cloudGray)
// Run Action to fade away
self.runAction(fadeToGray)
相当多的代码。我希望这个想法能够被接受。您可以调整动作中的时间或使用
fadeAlphaTo/By
代替 fadeIn/OutWithDuration
来产生所需的效果。
我这样做了,我制作了自己的自定义颜色,然后用它着色:
let customAction = SKAction.colorize(with: UIColor(red: 255.0/255, green: 204.0/255, blue: 0.0/255, alpha: 1.0), colorBlendFactor: 1,uration: 1) //这是金色
node.run(customAction)
SKColor
正在杀死所有其他像素,而不是所选颜色的像素。因此,如果您的图像不包含任何红色像素,则 [SKColor redColor]
将不起作用。我建议你使用CIHueAdjust
,这是一个CIFilter
。