Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

objective c - sprite kit sprite not being colorized

I'm trying to colorize a spriteNode (in this case its named background) using sprite kit but cant get the color to change. I have a sprite that I want to colorize. I'm changing the color property of the sprite as well as the color blending factor. When I run the app nothing happens though. the color remains the same (its black right now). below is the code I'm using to change the color:

background.anchorPoint = CGPointMake(0, .5);
background.position = position;

// ...

// this does not seems to be doing anything at all
background.color = [SKColor redColor];
background.colorBlendFactor = 1.0f;

did I miss something? I read through the docs and it seems like a pretty simple process. I believe all I need were those 2 lines, but it doesnt have any effect. Does anyone know why this would not be working? The texture image has a lot of transparency, could that be causing the issue? Ive tried this on different sprites and it never works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this:

    bug.color = SKColorWithRGB(128, 128, 128); 
    bug.colorBlendFactor = 1.0f;

Should definitely tint the sprite of choosing. The thing is that this is non intensive blend. Using a CIFilter with an SKEffectsNode is very very intensive for such a simple task.

I have an example below that blends a sprite with a red tinting, making the the sprite red tinted rather it's original color of orange:

    - (instancetype)init
{
    if (self = [super init]) {
        self.physicsBody.categoryBitMask = PCFireBugCategory;
        self.physicsBody.collisionBitMask = PCPlayerCategory | PCWallCategory | PCBreakableCategory | PCBoundaryCategory;
        //Keeps the bugs from sliding too far when bumped
        self.physicsBody.linearDamping = 1;
        self.physicsBody.angularDamping = 1;
        self.color = [SKColor redColor];
        self.colorBlendFactor = 0.45;
    }

    return self;
}

I would also like to point out that this class is a subclass of SKSpriteNode.

Attached below is an example where I've applied the red tint using the exact code above. enter image description here

Raywenderlich has a nice quote describing blending effects:

Note: Because the tint color is multiplied by the original color to get the final result, if you set the color of a sprite to blue the end result won’t necessarily be blue – it depends on what the original color is. Because of this, if you want to dynamically set a sprite’s color to a specific color, it is convenient to make the source color of the sprite white (like the cat in Zombie Conga). If you want parts of your sprite to be different colors, you can also split your sprite into different parts, each of which you tint to a different color. For more information, see the Beat ‘Em Up Game Starter Kit available at raywenderlich.com.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...