这是我在 Cocos2D 中的第一个游戏。我使用的是Cocos2D 1.0.1。我想在移动的 Sprite 上添加文本,该文本应该居中对齐。我已经拍摄了一个带有文本的 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];
//创建 Action
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];” Sprite 的 Action 将使其居中对齐,但徒劳无功。
最佳答案
尝试在此处设置标签位置:
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);
您可能需要调整位置值,直到达到您想要的位置。
关于objective-c - 如何在 Cocos2D 中的移动 Sprite 上添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7966319/