Colorear un CCSprite sin usar solo sus colores principales

I have a CCSprite that was created from a png with transparent background.

I want to be able to apply colors to this sprite in a way that I`m free to define which color it is, without the actual color of the sprite affecting the amount of each color I have to add.

I`ve tried this:

mySprite.color = ccc3(200,200,255);

In an attempt to add a little blue-ish feel to my sprite, but as it works by setting the amount of tint that's gonna be displayed based on existant color of the sprite, and my sprite has virtually no blue in any of it (most of it is yellow) the resulting effect is pretty sketchy, everything gets really dark, and there is one slight blue-ish coloring, but not as I wanted.

The ideal effect for me on this case would be to ADD a light blue mask to it with very low alpha.

Is there an easy way to do that without composing sprites?

I've tried using CCTexture2D, but had no luck, as there is no built in method for working with colors, and most tutorials only teach you how to build textures out of image files.

preguntado el 15 de noviembre de 11 a las 10:11

2 Respuestas

This is deceptively hard to do in code with the original sprite. Another option would be:

  • create a new sprite, which is just a white outline version of your original sprite
  • the color property of this white sprite will now respond exactly to the RGB values you pass in
  • so pass in your light blue value to the white sprite and set the opacity correctly
  • then overlay it on your original sprite

¿Ningún bien?

respondido 16 nov., 11:16

Thanks for the answer! I was trying to avoid doing that, but since CCSprite is so primitive, I'll give it a try. A couple of questions raise then: How do I create this white clone, since my image is a square PNG with an image, and transparent background? Can I create a sprite with these two resulting sprites or blend them into any single object? I move sprites around a lot, and it would be a pain to track them both and do the same thing for both. - Marcelo

Ah yes, I see now that you wanted to avoid composing sprites! Oh well. Anyway, you can add children to CCSprites. These children then honour anything that's done to the parent. I'm doing the same thing to put a border and shadow around some of my sprites, although in my case I'm putting the accessory sprite behind the original, whereas you would be putting yours in front. To make your white sprite in Photoshop, use... magic wand in transparent area -> CTRL+SHIFT+I -> delete -> fill with white. Then in Cocos2D create it as normal, set position to centre of original sprite, and add as child. - danyal aytekin

Thanks! that should work! Too late to try now, but I'll try it out tomorrow and give you the feedback! - Marcelo

The only way you can achieve this is by overlaying (masking) the sprite with the proper OpenGL blend functions. When you say "add a light blue mask" then that's what you need to do. You may find this visual blendfunc tool helpful, if only to understand how blending with mask sprites works and what you can achieve with it. There's no built-in support for this by Cocos2D however, so you'll have to revert to pure OpenGL inside a cocos2d node's -(void) draw {} método.

Tinting (changing the color property) will only modify the RGB channels of the entire image by changing the vertex colors of all 4 vertices.

respondido 15 nov., 11:15

Thanks for the answer! As I'm new to iOS development, let me se if I got it: What you said is basically to inherit Cocos2d CCSprite class, override its draw method, call super's init on it, and do the dirty job just below it? Can you draft a couple of lines of how this hacking would look like just for me to get the idea ? - Marcelo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.