views:

928

answers:

3

Is there a way in cocos2d 0.7.1 to specify the text alignment in a LabelAtlas?

I'm using a LabelAtlas for a score (displayed in the upper right corner) but when the score goes above 10, the second digit is cut off.

I can implement code to detect that and move the LabelAtlas, but is there a way to have cocos2d do it for me?

Thanks.

A: 

FWIW, I did end up writing code to do this.

 if(delegate.score > 99) {
  [scoreLabel setPosition:ccp(374, 265)];
 } else if(delegate.score > 9) {
  [scoreLabel setPosition:ccp(410, 265)];
 }
John
A: 

While that works, you'll have the same problem if the score goes above 1000.

You can use the anchorPosition property to change where the position is defined. For example; in my game I place the scoreLabel in the lower right hand corner of the screen. To ensure it is always visible I set anchorPosition to be the lower right hand corner of the label and then set its position where I want the lower right hand corner of the label to be.

[scoreLabel setAnchorPoint:ccp(1, 0)];
[scoreLabel setPosition:ccp(480, 0)];

If you want it positioned in the upper right hand corner of the screen then you can similarly use the following code to define the anchor as the upper right corner.

[scoreLabel setAnchorPoint:ccp(1, 1)];
[scoreLabel setPosition:ccp(480, 320)];
James Johnson
Oh, right! Since then, I have used setAnchorPoint in another project. Thanks.
John
+1  A: 

I simply use this to center my LabelAtlas:

[scoreLabel setAnchorPoint:ccp(.5, .5)];

When the score changes, the label stays center aligned without updating the position.