cocos2d: LabelAtlas text alignment?

拜拜、爱过 提交于 2019-12-24 10:24:54

问题


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.


回答1:


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



回答2:


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.




回答3:


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



回答4:


http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:labels#alignment



来源:https://stackoverflow.com/questions/787054/cocos2d-labelatlas-text-alignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!