Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Sprite Kit why can't I repeat colorizeWithColor using white color?

I'm experimenting with ways of selecting sprite nodes using methods other than scale. The one method that I like the most is colorize with white, which highlights the node visibly.

However, I cannot seem to be able to replicate the colorize with white behavior more than once. Why can't I apply colorizeWithColor using white color more than once?

These two method calls are identical, except for the color used. If I use red, gray, etc, the node responds by flashing for each touch. But If I use white, it does so only once, then never responds to touches again.

[self runAction:[SKAction colorizeWithColor:[SKColor lightGrayColor] colorBlendFactor:0.8 duration:0.6] completion:^{

        [self runAction:[SKAction colorizeWithColorBlendFactor:0.0 duration:0.4]];
    }];

    [self runAction:[SKAction colorizeWithColor:[UIColor colorWithWhite:0.99 alpha:1.0] colorBlendFactor:0.8 duration:0.6] completion:^{

        [self runAction:[SKAction colorizeWithColorBlendFactor:0.0 duration:0.4]];
    }];
like image 730
Alex Stone Avatar asked Dec 21 '13 03:12

Alex Stone


2 Answers

This is very interesting - and I'm not sure I have the answer. Using a white colorisation does to affect a node differently from other colours.

if you perform a colorize on a sprite node with blueColor and watch in the simulator, the colour will remain.

[node runAction:[SKAction colorizeWithColor:[SKColor blueColor] colorBlendFactor:0.8 duration:0.6]];

However, if you perform a colorize on a sprite node with whiteColor and watch in the simulator, it appears to automatically unwind (even without any completion block).

[node runAction:[SKAction colorizeWithColor:[SKColor whiteColor] colorBlendFactor:0.8 duration:0.6]];

I can find no reference to why this might be the case in documentation/header files. Still searching.

like image 194
So Over It Avatar answered Oct 18 '22 10:10

So Over It


I suggest you make a method to colorize, that returns you SKAction to use, for example:

-(SKAction*)colorizeChoosenSpriteNodeWithColor:(SKColor*)color
{
  SKAction *changeColorAction = [SKAction colorizeWithColor:color colorBlendFactor:1.0 duration:0.3];
  SKAction *waitAction = [SKAction waitForDuration:0.2];
  SKAction *startingColorAction = [SKAction colorizeWithColorBlendFactor:0.0 duration:0.3];
  SKAction *selectAction = [SKAction sequence:@[changeColorAction, waitAction, startingColorAction]];
  return selectAction;
}

And it is important to say if you are using SKSpriteNode that is made from SKColor or from an image. If you are trying to colorize SKSpriteNode that is made like:

SKSpriteNode *node = [[SKSpriteNode alloc]initWithColor:[SKColor redcolor] size:CGSizeMake(8, 8)];

By running colorize Action you will change that color and with that "startingColorAction" from above you will not make it to the recent color.

As it says in documentation: "Creates an animation that animates a sprite’s color and blend factor." So by running this Action to a SKSpriteNode that is made just from SKColor it will change the color and running the action with colorblendfactor 0.0 it will do nothing.

Use this action for colorizing sprite nodes made from images. Try testing it and see what happens.

And please, please read the "Sprite Kit Programming Guide" first!

like image 35
BSevo Avatar answered Oct 18 '22 10:10

BSevo