Jacob Peterson
Jacob Peterson

Reputation: 343

With xcode/swift how do I change the color of my SKSpriteNode that is a black image to white?

I have created a game with an SKSpriteNode that is a black image and when the user touches the screen I would like for the SKSpriteNode to change to white.

I have tried to use this:

sprite.color = .whiteColor()
sprite.colorBlendFactor = 1

And it does nothing. I have also tried something like:

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

But this just makes the sprite fade to white and then back to black.

How do I make my black sprite image turn white and stay white when the user touches the screen?

Upvotes: 2

Views: 1900

Answers (1)

0x141E
0x141E

Reputation: 12753

Changing the color property of a sprite with a black texture will have no effect. Also, setting a sprite's color property to .whiteColor() will have no effect, regardless of the sprite's original color. That said, there's at least one way to transition the color of a sprite from black to white.

First, start with a sprite with a white image (you can the change the color of the black image to white in an image editing app):

let sprite = SKSpriteNode(imageNamed: "whitebird")

Next, set the sprite's color property to black and the colorBlendFactor to 1

sprite.color = .blackColor()
sprite.colorBlendFactor = 1.0

Finally, in touchesBegan recolor the sprite from black to white by changing the colorBlendFactor from 1 to 0:

let recolor = SKAction.colorizeWithColorBlendFactor(0, duration: 2)
sprite.runAction(recolor)

Upvotes: 2

Related Questions