0

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)

Sebastian
  • 7,670
  • 5
  • 38
  • 50
RaphBlanchet
  • 575
  • 6
  • 23

1 Answers1

3

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.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
  • Yeah I know, I was sure someone would tell me that, but is it really that awful not to create a subclass for just one parameter? Sorry, I'm really new to coding ^^" And between, thanks for your answer, it solved it :D I now realize that is a really n00bish question... – RaphBlanchet Jan 18 '13 at 04:46
  • 1
    If you need a different set of defaults it's ok to modify the source code. But you're on the safer side with a subclass. Why? Because if you ever wanted to update cocos2d, you'd have to redo all your custom modifications. And will you remember them at that time? Probably not, unless you're using source control which may (or may not) be able to merge your changes into the new version. – CodeSmile Jan 18 '13 at 09:07
  • Besides in Objective-C you should prefer to create a category on the class instead of subclassing unless you need to add extra ivars to the class. – CodeSmile Jan 18 '13 at 09:09