如何在Cocos2D中移动精灵上添加文本?

时间:2011-11-01 12:20:12

标签: objective-c ios cocos2d-iphone

这是我在Cocos2D的第一款游戏。我正在使用Cocos2D 1.0.1。我想在移动的精灵上添加文本,这些精灵应该居中对齐。我带了一个带有文字的CCLabelTTF,但我无法使它居中对齐。这就是我到目前为止所做的: -

- (void)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];”精灵的行动将使它居中对齐,但徒劳无功。

2 个答案:

答案 0 :(得分: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);

您可能需要使用位置值,直到获得所需位置值。

答案 1 :(得分:0)

您无需更改标签的位置,所有内容都根据图像的中心放置。另外,有了这个

[target addChild:label z: 10];

尝试将按钮设置为z:11,并将标签保持在z:10

相关问题