colorscocos2d-iphonecclabelttf

CCLabelTTF - Changing Default Color of the label


I've just upgraded my project from cocos2d 1.0.1 to 2.0 and after a lot of tweaking and all, I'm unable to change the default color of a CCLabelTTF like I did before (And this way I avoid one line of code for each label I create). Before, I was doing like that :

In CCLabelTTF.m :

- (id) initWithString:(NSString*)str dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment lineBreakMode:(CCLineBreakMode)lineBreakMode fontName:(NSString*)name fontSize:(CGFloat)size
{
  if( (self=[super init]) ) {

    dimensions_ = CGSizeMake( dimensions.width * CC_CONTENT_SCALE_FACTOR(), dimensions.height * CC_CONTENT_SCALE_FACTOR() );
    alignment_ = alignment;
    fontName_ = [name retain];
    fontSize_ = size * CC_CONTENT_SCALE_FACTOR();
    lineBreakMode_ = lineBreakMode;
    color_ = ccBLACK;

    [self setString:str];
  }
  return self;
}

I was changing the color inside this method since every "initWithString..." methods are returning this one, but even if I do so in cocos2D 2.0, it doesn't work.

Here's my new CCLabelTTF.m :

- (id) initWithString:(NSString*)str dimensions:(CGSize)dimensions hAlignment:(CCTextAlignment)alignment vAlignment:(CCVerticalTextAlignment) vertAlignment lineBreakMode:(CCLineBreakMode)lineBreakMode fontName:(NSString*)name fontSize:(CGFloat)size
{
  if( (self=[super init]) ) {

    // shader program
    self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:SHADER_PROGRAM];

    dimensions_ = dimensions;
    hAlignment_ = alignment;
    vAlignment_ = vertAlignment;
    fontName_ = [name retain];
    fontSize_ = size;
    lineBreakMode_ = lineBreakMode;
            color_ = ccBLACK;

    [self setString:str];
  }
  return self;
}

Is it because of the "ShaderProgram" thingy that wasn't there before 2.0? Please help I've tried everything so far :(

I even searched in all my project if there was a file containing "ccWHITE" or "{255,255,255}" but there's none related to CCLabelTTF (except for CCSprite, but if I change it to ccBLACK, all my sprites becomes black)


Solution

  • Instead of setting the ivar, use the accessor for the property:

    self.color = ccBlack;
    

    Also, you should not modify CCLabelTTF. If you want to change behaviour, make a subclass.