1

I have created png images of several shapes, such as hexagons and stars, and they are all white color.

I want to colorize these SKSpriteNodes or SKTextures so that I can assign any color rather than creating shape images for each color I need and loading those textures into memory. I need the images to be high resolution, so having several images and loading them into memory is not a good option since I receive memory warnings from XCode. Loading them into memory as they are needed also does not work because they cause my game to lag for about 0.25 seconds.

So, is there any way I can create 1 SKTexture and colorize the image texture of an SKSpriteNode whenever I need it to change colors?

Devin Reid
  • 13
  • 2

1 Answers1

2

You can change color of white texture like this (assuming that you are using pure white textures):

//Colorize to red

 let yourNode = SKSpriteNode(imageNamed: "textureName")
 yourNode.colorBlendFactor = 1
 yourNode.color = UIColor.redColor()

About 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.

More about this topic on StackOverflow.

Hope this helps.

Community
  • 1
  • 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Thank you! This is so simple but extremely helpful. I was trying multiple variations of texture properties and couldn't get it right. This is going to save me a TON of work and memory in my app. Thanks again! – Devin Reid Jul 21 '15 at 01:28