1

I am probably staring the answer in the face, however.

I want to assign a random colour to a CCLabelTTF string. When I try to set the return type of (CCColor3B *) and assign it CCLabelTTF.color = [self randomColor] I get incompatible assignment errors, both in the method, and at the above assignment. Method code:

-(ccColor3B *)randomColor
{
float r = arc4random() % 255;
float g = arc4random() % 255;
float b = arc4random() % 255;
ccColor3B *color =  {r,g,b,1};
return color;
}

I think I am trying to obtain a return value which is the wrong type, or assign a read-only value, but information on CCColor3B is scarce. Thanks in advance.

Jesse Black
  • 7,966
  • 3
  • 34
  • 45
Skybird
  • 159
  • 1
  • 11

1 Answers1

3

From Cocos2d class documentation, the property color of CCSprite is not a pointer (it is a struct)

-(ccColor3B) color [read, write, assign]

You need to change your method as follows

-(ccColor3B)randomColor
{
float r = arc4random() % 255;
float g = arc4random() % 255;
float b = arc4random() % 255;
return ccc3(r,g,b);
}

You can find the definition of ccColor3B in the docs for CCTypes.h (line 43)

typedef struct _ccColor3B {
GLubyte r;
GLubyte g;
GLubyte b; } ccColor3B;

Jesse Black
  • 7,966
  • 3
  • 34
  • 45
  • There's also the shortcut method of ccc3(r,g,b). Your code also has an error where it uses the alpha component which ccColor3B doesn't have: {r,g,b,1} should be {r,g,b} – CodeSmile Jan 14 '12 at 15:16
  • @LearnCocos2D Thank you, I missed that because I was focusing on the pointer mistake from OP. I can't believe I missed that when I posted the typedef right there – Jesse Black Jan 14 '12 at 18:32