2012-08-28 20 views
0

我有一個簡單的CCLayer子類與圓形作爲背景圖像。我還有一個CCLabelTTF作爲子節點。 如何將標籤的文字置於背景圖片上?居中在CCSprite CCLabelTTF

這是我有:

// in MyCCLayer's init method 
CCSprite *backgroundImage = [CCSprite spriteWithFile:@"circle.png"]; 
[self addChild:backgroundImage]; 
self.contentSize = backgroundImage.contentSize; 

CCLabelTTF *label = [CCLabelTTF labelWithString:@"0" 
             dimensions:self.contentSize 
              alignment:UITextAlignmentCenter 
              fontName:@"Arial" 
              fontSize:32]; 
[self addChild:label]; 

我試圖改變anchorPoint和位置的標籤上,但我不能只是將背景圖像上居中的文字。文字總是偏移。無論字體大小如何,我都希望讓文字居中。

回答

2

我假設看着你的代碼,你的標籤已經結束了在圓的底部?如果您按照以下方式創建標籤,它應該適合您。

CCLabelTTF *label = [CCLabelTTF labelWithString:@"0" fontName:@"Arial" fontSize:32]; 
label.anchorPoint = ccp(0.5, 0.5); 
label.position = ccp(backgroundImage.contentSize.width/2, backgroundImage.contentSize.height/2); 
0
try this code: 

    CCSprite *backgroundImage = [CCSprite spriteWithFile:@"circle.png"]; 
    [self addChild:backgroundImage]; 

    backgroundImage.position=ccp(winSize.width/2,winSize.height/2); 

    CCLabelTTF *label = [CCLabelTTF labelWithString:@"0" 
            dimensions:self.contentSize 
             alignment:UITextAlignmentCenter 
             fontName:@"Arial" 
             fontSize:32]; 
    label.position=backgroundImage.position; 
    [self addChild:label];