2013-03-12 45 views
0

我期望以下代码将标签放置在屏幕的右上角。然而,x位置显示在屏幕的右侧,我必须手动移动它。这段代码有什么问题?对齐并定位CCLabelTTF

CGSize winSize = [[CCDirector sharedDirector] winSize]; 

self.screenTop = winSize.height; 
self.screenRight = winSize.width; 
self.screenBottom = 0; 
self.screenLeft = 0; 


self.scoreLabel = [CCLabelTTF labelWithString:@"000000" dimensions:CGSizeMake(200,30) hAlignment:kCCTextAlignmentRight fontName:@"Helvetica" fontSize:24]; 
self.scoreLabel.position = ccp(self.screenRight - 20, self.screenTop - 20); 
[self addChild:self.scoreLabel z:1]; 

该位置记录正确为{548,300}(iPhone5)。看起来hAlignment:kCCTextAlignmentRight不能正常工作。

+0

'horizo​​ntalAlignment'在其边框的文本的对齐方式。如果你有多行文字,它才有用。 – 2013-03-12 14:28:51

回答

0
self.scoreLabel.position = ccp(self.screenRight - 100, self.screenTop - 20); 

更一般:

self.scoreLabel.position = ccp(self.screenRight - self.scoreLabel.contenSize.width/2, 
self.screenTop - self.scoreLabel.contenSize.height/2); 
+6

...或者anchorPoint 1,1 – LearnCocos2D 2013-03-12 01:41:53

+0

true ...甚至更通用:) – YvesLeBorg 2013-03-12 02:31:24

+0

我本来会期望设置right-align会自动将定位点设置为1,1,但我估计不会。 – soleil 2013-03-12 03:25:14