cocos2d-iphonecclayercclabelttfccnode

Set position of CCLabelTTF in CCNode and add to CCScene [cocos2d v3]


I'm having trouble understanding what is going on here. I currently have a "Header" class that inherits from CCNode. This CCNode class has multiple properties including CCSprite's and CCLabelTTF's. In the init, I create these objects and set their positions. I then include this Header class in my scene and add it as a child to the scene. However, the label position is not set correctly. It sets the position attribute of my label to 0, 0.

Here's the code for the Header.h:

@interface Header : CCNode {
    CCButton *settingsButton_;
    CCButton *crewButton_;
    CCButton *goldButton_;
    CCSprite *divider_;
    CCLabelTTF *rank_;
    CCLabelTTF *userName_;
}

@property (nonatomic, retain) CCButton *settingsButton;
@property (nonatomic, retain) CCButton *crewButton;
@property (nonatomic, retain) CCButton *goldButton;
@property (nonatomic, retain) CCSprite *divider;
@property (nonatomic, retain) CCLabelTTF *rank;
@property (nonatomic, retain) CCLabelTTF *userName;

+ (id)node;
- (id)init;

@end

And Header.m:

@implementation Header

+(id)node {
    return [[self alloc] init];
}

-(id)init {
    self = [super init];
    if (!self) return(nil);

    NSString *rankString = [[GameManager sharedGameManager] getRank];

    self.rank = [CCLabelTTF labelWithString:rankString fontName:@"Copperplate-Bold" fontSize:16.0];
    self.rank.color = [CCColor blackColor];
    self.rank.positionType = CCPositionTypeNormalized;
    self.rank.position = ccp(0.35, 0.965);

    [self addChild:self.rank];

    return self;
}

@end

Now when I add this Header CCNode to my layer, the position does not stay at ccp(0.35, 0.965), it just resets to 0,0.

Is there something I'm missing here?


Solution

  • Problem was the CCPositionNormalized. If you use that, it does not take actual pixel sizes. Once I commented that out and used actual pixel sizes (ccp(100, 200) for example) it worked fine. Thanks!