【问题标题】:How to add text on moving sprites in Cocos2D?如何在 Cocos2D 中的移动精灵上添加文本?
【发布时间】:2011-11-02 00:43:41
【问题描述】:

这是我在 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];”在精灵的作用下,它会使其居中对齐,但徒劳无功。

【问题讨论】:

  • 当标签是目标的子标签时,不需要设置标签的位置。根据我在这里收集的信息,当您移动目标时,您的标签会缩小
  • @James:- 我删除了“[label setPosition:target.position];”。它仍然没有进入中心。现在我正在编辑我的代码,因为我已将其从操作中删除。

标签: objective-c ios cocos2d-iphone


【解决方案1】:

尝试在此处设置标签位置:

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);

您可能不得不使用位置值,直到将它放在您想要的位置。

【讨论】:

  • 你做到了 :) 我采取了“label.position = ccp(55,20);”它成功了。非常感谢!!!
【解决方案2】:

您不需要更改标签的位置,所有内容都基于图像的中心放置。另外,有了这个

[target addChild:label z: 10];

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

【讨论】:

    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多