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

后端 未结 2 1979
心在旅途
心在旅途 2021-02-05 15:43

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 h

2条回答
  •  梦谈多话
    2021-02-05 15:53

    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.

提交回复
热议问题