gpt4 book ai didi

ios - 如何快速更改 SKSpriteNode 的颜色?

转载 作者:IT王子 更新时间:2023-10-29 05:25:07 26 4
gpt4 key购买 nike

我创建了一个带有黑色 SKSpriteNode 的游戏,当用户触摸屏幕时,我希望 SKSpriteNode 变为白色。我用谷歌搜索了一切,并尝试了许多不同的策略,但没有成功。有谁知道如何做到这一点?

这是我的场景代码:

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
}

最佳答案

您可以在SKSpriteNode 上使用color 属性,例如:

sprite.color = .whiteColor()

请记住,如果您的 SKSpriteNode 有纹理,您需要将 colorBlendFactor 设置为非零值才能看到您的颜色。来自SKSpriteNode documentationcolorBlendFactor 上:

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.


如果你想动画化颜色变化,你可以使用 SKAction:

let colorize = SKAction.colorizeWithColor(.whiteColor(), colorBlendFactor: 1, duration: 5)
sprite.runAction(colorize)

来自SKAction documentationcolorizeWithColor: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.

关于ios - 如何快速更改 SKSpriteNode 的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31484411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com