I have a sprite (healthbar) in my scene and I wish to colorize it whenever the player gets hit. I can colorize my sprite with greenColor, and blueColor. However, redColor, brownColor, yellowColor, cyanColor, and whiteColor all don't seem to work.
Sprite:
_healthBar
Code:
SKAction *pulseRed = [SKAction sequence:@[
[SKAction colorizeWithColor:[SKColor redColor] colorBlendFactor:1.0 duration:0.15],
[SKAction waitForDuration:0.1],
[SKAction colorizeWithColorBlendFactor:0.0 duration:0.15]]];
SKAction *repeat = [SKAction repeatActionForever:pulseRed];
[_healthBar runAction: repeat];
Output:
[SKColor redColor] Not Working
Other Colors:
[SKColor yellowColor] Not Working
[SKColor blueColor] Working
[SKColor greenColor] Working
The yellow seems to make sense, since yellow and blue make green. However, I don't think this is what's happening here.
Any thoughts?
EDIT:
There is a workaround, which other people have hinted at below, that works great with rectangular sprites (basically create a colored sprite on top of your textured one). However, I would like to find a solution that not just for rectangular sprites, since I will be using this strategy to colorize other sprites in my game.
Solution for colorizing a rectangular sprite (Ahmet Cevdet):
@interface
@property (strong, nonatomic) SKSpriteNode *filterNode;
@implementation
CGSize filterSize = CGSizeMake(_healthBar.size.width, _healthBar.size.height);
SKColor *filterColor = [SKColor redColor];
_filterNode = [SKSpriteNode spriteNodeWithColor:filterColor size:filterSize];
_filterNode.alpha = 0;
[_healthBar addChild:_filterNode];
SKAction *fadeIn = [SKAction fadeAlphaTo:.6 duration:.15];
SKAction *fadeOut = [SKAction fadeAlphaTo:0 duration:.15];
SKAction *wait = [SKAction waitForDuration:0.1];
[_filterNode runAction:[SKAction repeatActionForever:[SKAction sequence:@[fadeIn,wait,fadeOut]]] withKey:@"pulseRed"];
[SKColor whiteColor]flash to make it flash white. It did literally nothing. I think what it does is add or subtract the specific colour from the sprite, or something like that. I can't explain exactly but the way I solved it was by adding a sprite of the same size with the desired colour and placing it over the top of the health bar and then fading the alpha in and out. A bit of a dodgy workaround but it worked. - Andy Heard