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
}
CodeBender
  • 35,668
  • 12
  • 125
  • 132
Jacob Peterson
  • 343
  • 1
  • 4
  • 12

2 Answers2

34

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.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Hey thanks for the reply. I'm trying the first option you gave me like this: sprite.color = .whiteColor() sprite.colorBlendFactor = 1.0 It's not working for me, am I doing it wrong? Thanks! – Jacob Peterson Jul 17 '15 at 22:15
  • What exactly isn't working? Are you getting any errors? Is it not changing color? Your sprite's texture isn't black is it? – ABakerSmith Jul 17 '15 at 22:21
  • Sorry, no errors it's just not changing color. My sprite is an image i uploaded that is currently black and I'm trying to change it to white on touch. I'm not sure if that means its current texture is black? Also, I did try the second option you gave me and it did work by the image slowly becoming white and then going back to black. I just need the image to stay white. Thanks again! – Jacob Peterson Jul 17 '15 at 22:31
  • Do you think you could post the relevant code - the creation of the `SKSpriteNode`; where you're trying to set the color and in which method? – ABakerSmith Jul 17 '15 at 22:33
  • I've added your answer as an edit to your question - that's where it belongs really (you should remove your answer too) :) Regarding your issue - is the `blackbird` image black coloured? – ABakerSmith Jul 17 '15 at 22:56
  • White blended with an image produces the original image. You'll have better luck with `.redColor`. – 0x141E Jul 18 '15 at 03:32
  • Not only did trying a .redColor() not work but that is not what i need. I need my black image to turn white and stay white. Thanks though. – Jacob Peterson Jul 18 '15 at 20:55
11

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/

Myoch
  • 793
  • 1
  • 6
  • 24
  • Sorry, just edited. The quote is the part that allowed me solving the same issue as described in this topic. – Myoch Aug 31 '15 at 17:09