In swift how do I change the color of a SKSpriteNode?

社会主义新天地 提交于 2019-12-03 12:04:17

问题


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?

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
}

回答1:


You can use the color property on SKSpriteNode, for example:

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 documentation on 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.


If you want to animate the color change you can use an SKAction:

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

From the SKAction documentation on 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.




回答2:


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.

link: https://www.reddit.com/r/swift/comments/30hr88/colorizing_a_skspritenode_doesnt_seems_to_work_as/



来源:https://stackoverflow.com/questions/31484411/in-swift-how-do-i-change-the-color-of-a-skspritenode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!