Cocos 2D CCColor3b not assignable to property.color

2.7k views Asked by At

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.

1

There are 1 answers

2
Jesse Black On BEST ANSWER

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;