0

CCLayer背景画像として円を持つ単純なサブクラスがあります。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];

ラベルのアンカーポイントと位置を変更しようとしましたが、テキストを背景画像の中央に配置することはできません。テキストは常にオフセットされます。フォントサイズに関係なく、テキストを中央揃えにしたいと思います。

4

2 に答える 2

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);
于 2012-08-30T01:01:44.067 に答える
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];
于 2014-01-06T14:05:33.377 に答える