Sprite-Kit colorizeWithColor不会将精灵与正确的颜色混合

时间:2014-07-18 02:46:39

标签: ios objective-c colors sprite-kit

我的场景中有一个精灵(健康栏),我想在玩家被击中时将其着色。我可以使用greenColor和blueColor为我的精灵着色。但是,redColor,brownColor,yellowColor,cyanColor和whiteColor似乎都不起作用。

的Sprite:

enter image description here _healthBar

代码:

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];

输出:

enter image description here [SKColor redColor]无效

其他颜色:

enter image description here [SKColor yellowColor]无效

enter image description here [SKColor blueColor]工作

enter image description here [SKColor greenColor]工作

黄色似乎有意义,因为黄色和蓝色变绿。但是,我不认为这就是这里发生的事情。

有什么想法?


修改

有一个解决方法,其他人在下面暗示过,它适用于矩形精灵(基本上在纹理精灵顶部创建一个彩色精灵)。但是,我想找到一个不仅适用于矩形精灵的解决方案,因为我将使用此策略为游戏中的其他精灵着色。

为矩形精灵着色的解决方案(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 个答案:

答案 0 :(得分:3)

以下是您需要的所有代码,我希望它有所帮助;

@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