2011-11-01 65 views
2

這是我第一次參加Cocos2D遊戲。我正在使用Cocos2D 1.0.1。我想在移動的應該居中對齊的精靈上添加文本。我已經拿了一個帶有文本的CCLabelTTF,但是我無法讓它居中對齊。這是我到目前爲止已經完成: -如何在Cocos2D中添加移動精靈上的文本?

- (無效)addTarget

帶我讀過{

int enType= arc4random() % 11; 

CCSprite *target=[CCSprite spriteWithFile:[NSString stringWithFormat:@"balloon%d.png",enType] rect:CGRectMake(0, 0, 100, 119)]; 

label = [[CCLabelTTF alloc] initWithString:@"H!" dimensions:CGSizeMake([target contentSize].width, [target contentSize].height) 
           alignment:UITextAlignmentCenter fontName:@"verdana" fontSize:20.0f]; 

label.color = ccc3(60,60,60); 

[target addChild:label z: 10]; 

//創建

id actionMove = [CCMoveTo actionWithDuration:rangeDuration position:ccp(actualX,winSize.height+target.contentSize.height)]; 

[target runAction:[CCSequence actions:actionMove, nil]]; 

//[label setPosition:target.position]; 


// Add to targets array 

[targets addObject:target]; 

行動

}添加「[label setPosition:target.position];」在精靈的行動將使它中心對齊,但徒勞無功。

+1

你並不需要設置標籤的位置時,它的目標的孩子。從我在這裏收集的信息來看,當您移動目標 –

+0

@ James時,您的標籤將會縮小: - 我刪除了「[label setPosition:target.position];」。它仍然不在中心。現在,我正在編輯我的代碼,因爲我已將其從操作中刪除。 –

回答

3

嘗試在這裏設置你的標籤位置,而不是:

label = [[CCLabelTTF alloc] initWithString:@"H!" dimensions:CGSizeMake([target contentSize].width, [target contentSize].height) 
           alignment:UITextAlignmentCenter fontName:@"verdana" fontSize:20.0f]; 
//LABEL POSITION HERE 
label.position = ccp(0, 40); 
label.color = ccc3(60,60,60); 

你可能有,直到你得到它,你想要的位置值的發揮。

+0

你做到了:)我採取了「label.position = ccp(55,20);」它做到了訣竅。非常感謝!!! –

0

您不需要更改標籤的位置,所有內容都基於圖像的中心放置。另外,在該

[target addChild:label z: 10]; 

試按鈕設置爲Z:11,並保持標籤在Z:10