xcode 如何快速更改 SKSpriteNode 的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31484411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In swift how do I change the color of a SKSpriteNode?
提问by Jacob Peterson
I have created a game with an SKSpriteNode
that is black and when the user touches the screen I would like for the SKSpriteNode
to change to white. I have googled everything I can and attempted lots of different strategies with no luck. Does anyone know how to do this?
我创建了一个SKSpriteNode
黑色的游戏,当用户触摸屏幕时,我希望将SKSpriteNode
其更改为白色。我已经尽我所能在谷歌上搜索并尝试了许多不同的策略,但都没有运气。有谁知道如何做到这一点?
Here's the code for my scene:
这是我的场景的代码:
var blackBird = SKSpriteNode()
override func didMoveToView(view: SKView) {
//Black Bird
var blackBirdTexture = SKTexture(imageNamed:"blackbird")
blackBirdTexture.filteringMode = SKTextureFilteringMode.Nearest
blackBird = SKSpriteNode(texture: blackBirdTexture)
blackBird.setScale(0.5)
blackBird.position = CGPoint(x: self.frame.size.width * 0.35, y:
self.frame.size.height * 0.6)
blackBird.physicsBody =
SKPhysicsBody(circleOfRadius:blackBird.size.height/2.0)
blackBird.physicsBody!.dynamic = true
blackBird.physicsBody!.allowsRotation = false
self.addChild(blackBird)
}
override func touchesBegan(touches: Set<NSObject, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
blackBird.color = .whiteColor()
blackBird.colorBlendFactor = 1.0
}
回答by ABakerSmith
You can use the color
property on SKSpriteNode
, for example:
您可以在color
上使用该属性SKSpriteNode
,例如:
sprite.color = .whiteColor()
Bear in mind, if your SKSpriteNode
has a texture you'll need to set the colorBlendFactor
to a non-zero value to see your color. From the SKSpriteNode
documentationon colorBlendFactor
:
请记住,如果您SKSpriteNode
有纹理,则需要将 设置为colorBlendFactor
非零值才能看到您的颜色。从SKSpriteNode
文档中colorBlendFactor
:
The value must be a number between 0.0 and 1.0, inclusive. The default value (0.0) indicates the color property is ignored and that the texture's values should be used unmodified. For values greater than 0.0, the texture is blended with the color before being drawn to the scene.
该值必须是介于 0.0 和 1.0(含)之间的数字。默认值 (0.0) 表示颜色属性被忽略,纹理的值应该不加修改地使用。对于大于 0.0 的值,纹理在被绘制到场景之前与颜色混合。
If you want to animate the color change you can use an SKAction
:
如果要为颜色变化设置动画,可以使用SKAction
:
let colorize = SKAction.colorizeWithColor(.whiteColor(), colorBlendFactor: 1, duration: 5)
sprite.runAction(colorize)
From the SKAction
documentationon colorizeWithColor:colorBlendFactor:duration:
从SKAction
文档中colorizeWithColor:colorBlendFactor:duration:
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.
此操作只能由 SKSpriteNode 对象执行。当动作执行时,精灵的 color 和 colorBlendFactor 属性被动画化为它们的新值。
回答by Myoch
Copying an answer found on another forum:
复制在另一个论坛上找到的答案:
The black parts of your image won't be blended, I can't see what the alpha is from your image, but if your image was just a black outline with a solid white center, if you blend, you'll see the color. If you want just the outline to blend, you need to create a white outline. With blending, you can set color to blend to black, but you can't blend to white.
图像的黑色部分不会混合,我看不到图像中的 alpha 值,但是如果您的图像只是带有纯白色中心的黑色轮廓,如果混合,您将看到颜色. 如果您只想混合轮廓,则需要创建一个白色轮廓。通过混合,您可以将颜色设置为混合为黑色,但不能混合为白色。
link: https://www.reddit.com/r/swift/comments/30hr88/colorizing_a_skspritenode_doesnt_seems_to_work_as/
链接:https: //www.reddit.com/r/swift/comments/30hr88/colorizing_a_skspritenode_doesnt_seems_to_work_as/