1

How can I add text or characters into a Sprite. If it's not possible, is there any alternative to get the same effect?

NOTE: I am using COCOS2D framework.

EDIT:

I have tried in this way-

CCLabel* label = [[CCLabel alloc] initWithString:@"H!" dimensions:CGSizeMake([spriteObj contentSize].width, [spriteObj contentSize].height)  alignment:UITextAlignmentCenter fontName:@"verdana" fontSize:15.0f];
//label.position = newBubble.position;//ccp([spriteObj contentSize].width/2, [spriteObj contentSize].height/2);
[spriteObj addChild:label z: 10];

It shows the text 'H!' at the bottom left corner of the sprite.

leppie
  • 115,091
  • 17
  • 196
  • 297
Sadat
  • 3,493
  • 2
  • 30
  • 45
  • I have succeed to add label to a sprite, but its not coming at center of sprite. Its setting outside the sprite :(. – Sadat Aug 04 '10 at 05:25

3 Answers3

1

As Sadat indicates, adding the label as a child of the sprite does the trick. It has the advantage over simply setting the sprite and label to the same position that, as a child, the label will take part in any animations you might apply to the sprite. For instance, the CCScaleTo animation will scale the label along with the sprite's image.

sharptalon
  • 11
  • 1
0

Just create the CCLabel like you are doing and set its position to the desired location by calling:[label setPosition:spriteObj.position]; That will move the label to the position of the sprite.

If your sprite is moving and you want the label to move along with it, place that line in your update method.

pabloruiz55
  • 1,306
  • 1
  • 14
  • 27
0

I have done it using addChild method of Sprite. The position is set with respect to the left bottom co-ordinate of that Sprite.

Note: I have used cocos2d.

Sadat
  • 3,493
  • 2
  • 30
  • 45