4
votes

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:

enter image description here _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:

enter image description here [SKColor redColor] Not Working

Other Colors:

enter image description here [SKColor yellowColor] Not Working

enter image description here [SKColor blueColor] Working

enter image description here [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"];
1
I encountered something like this with an [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
@Andy, you're right. If I change my sprite to a white bar, it works just fine. This bizarre effect only happens when the sprite is colored. - JRam13

1 Answers

3
votes

Here is the code that all you need, I wish it helps;

@implementation MyScene
{
    float filterParameter;
}

-(id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        SKSpriteNode *healthBar = [SKSpriteNode spriteNodeWithImageNamed:@"health"];
        healthBar.position = CGPointMake(self.size.width/2, self.size.height/2);

        SKEffectNode *effectNode = [SKEffectNode node];
        [effectNode addChild:healthBar];
        [self addChild:effectNode];

        filterParameter = 0;

        CIFilter *filter = [CIFilter filterWithName:@"CIHueAdjust"];
        [filter setValue:[NSNumber numberWithFloat:filterParameter] forKey:@"inputAngle"];
        effectNode.filter = filter;
        effectNode.shouldEnableEffects = YES;

        SKAction *wait =[SKAction waitForDuration:0.1];
        SKAction *block = [SKAction runBlock:^{
            filterParameter += 0.1;
            [filter setValue:[NSNumber numberWithFloat:filterParameter] forKey:@"inputAngle"];
            NSLog(@"%f", filterParameter);
        }];

        [effectNode runAction:[SKAction repeatActionForever:[SKAction sequence:@[block, wait]]]];

    }
    return self;
}

@end