9

I must be missing something!

I want to create a solid rectangular CCSprite with a background color initialized to a particular RGB value. I have looked all over the docs and can't find anything.

Is there a way to initialize the background of CCSprite to a specific color? I don't want to have to include a solid color PNG for each colors that I will need.

Help!

poundev23
  • 807
  • 3
  • 11
  • 18

5 Answers5

21

Do it with code! if you don't want to mess with image files, here's your method:

- (CCSprite*)blankSpriteWithSize:(CGSize)size
{
    CCSprite *sprite = [CCSprite node];
    GLubyte *buffer = malloc(sizeof(GLubyte)*4);
    for (int i=0;i<4;i++) {buffer[i]=255;}
    CCTexture2D *tex = [[CCTexture2D alloc] initWithData:buffer pixelFormat:kCCTexture2DPixelFormat_RGB5A1 pixelsWide:1 pixelsHigh:1 contentSize:size];
    [sprite setTexture:tex];
    [sprite setTextureRect:CGRectMake(0, 0, size.width, size.height)];
    free(buffer);
    return sprite;
}

Then you can set your color, size and opacity as needed. ;)

Matjan
  • 3,591
  • 1
  • 33
  • 31
  • 1
    For those of use working with the mutated counter part (that being cocos2d-x): https://gist.github.com/Mazyod/208e5fb2d7a56290a261 – Mazyod Aug 20 '14 at 07:02
9

CCSprite has a color property of type ccColor3B:

- (ccColor3B) color [read, assign]
RGB colors: conforms to CCRGBAProtocol protocol    

Definition at line 145 of file CCSprite.h.

Source: CCSprite reference.

You can easily construct a ccColor3B struct using ccc3():

ccc3(const GLubyte r, const GLubyte g, const GLubyte b)

Reference: ccColor3B reference.

Justin
  • 20,509
  • 6
  • 47
  • 58
  • 6
    Justin, thanks that was what I was doing ... the additional trick of course is setting the textureRect properly!!! – poundev23 Jul 29 '10 at 14:02
5

I found answer at cocos2d cookbook. The following code is derived from that book's chap 1, which is free for preview.

-(CCSprite *) rectangleSpriteWithSize:(CGSize)cgsize color:(ccColor3B) c
{
    CCSprite *sg = [CCSprite spriteWithFile:@"blank.png"];  
    [sg setTextureRect:CGRectMake( 0, 0, cgsize.width, cgsize.height)];
    sg.color = c;   
    return sg;  
}

Yes, this still requires an external image file. But with this 1x1 tiny 'blank.png', you can generate solid-color rectangle sprites with arbitrary size and color.

Tim Wu
  • 2,047
  • 2
  • 22
  • 32
3

I never got CCSprite to work like that. I just use CCLayerColor.

CCLayerColor* layercolorHalftransparentred = [CCLayerColor layerWithColor:ccc4(255, 0, 0, 128)];
Jonny
  • 15,955
  • 18
  • 111
  • 232
0

For anyone stumbling upon this question (like me); the code from Matjan doesn't seem to work anymore on cocos 2d 3.x. See below for an altered version that works for me:

+ (CCSprite*)blankSpriteWithSize:(CGSize)size
{
    GLubyte *buffer = malloc(sizeof(GLubyte)*4);
    for (int i=0;i<4;i++) {buffer[i]=255;}
    CCTexture *tex = [[CCTexture alloc] initWithData:buffer pixelFormat:CCTexturePixelFormat_RGBA8888 pixelsWide:1 pixelsHigh:1 contentSizeInPixels:size contentScale:1];
    CCSprite *sprite = [CCSprite spriteWithTexture:tex rect:CGRectMake(0,0,size.width,size.height)];
    free(buffer);
    return sprite;
}
Jeroen Bouma
  • 593
  • 6
  • 16