【问题标题】:Set position of CCLabelTTF in CCNode and add to CCScene [cocos2d v3]在CCNode中设置CCLabelTTF的位置并添加到CCScene [cocos2d v3]
【发布时间】:2014-02-19 02:09:46
【问题描述】:

我无法理解这里发生了什么。我目前有一个继承自 CCNode 的“Header”类。这个 CCNode 类有多个属性,包括 CCSprite 和 CCLabelTTF。在初始化中,我创建这些对象并设置它们的位置。然后,我将这个 Header 类包含在我的场景中,并将其作为子类添加到场景中。但是,标签位置设置不正确。它将我的标签的位置属性设置为 0, 0。

这是 Header.h 的代码:

@interface Header : CCNode {
    CCButton *settingsButton_;
    CCButton *crewButton_;
    CCButton *goldButton_;
    CCSprite *divider_;
    CCLabelTTF *rank_;
    CCLabelTTF *userName_;
}

@property (nonatomic, retain) CCButton *settingsButton;
@property (nonatomic, retain) CCButton *crewButton;
@property (nonatomic, retain) CCButton *goldButton;
@property (nonatomic, retain) CCSprite *divider;
@property (nonatomic, retain) CCLabelTTF *rank;
@property (nonatomic, retain) CCLabelTTF *userName;

+ (id)node;
- (id)init;

@end

还有 Header.m:

@implementation Header

+(id)node {
    return [[self alloc] init];
}

-(id)init {
    self = [super init];
    if (!self) return(nil);

    NSString *rankString = [[GameManager sharedGameManager] getRank];

    self.rank = [CCLabelTTF labelWithString:rankString fontName:@"Copperplate-Bold" fontSize:16.0];
    self.rank.color = [CCColor blackColor];
    self.rank.positionType = CCPositionTypeNormalized;
    self.rank.position = ccp(0.35, 0.965);

    [self addChild:self.rank];

    return self;
}

@end

现在当我将这个 Header CCNode 添加到我的层时,位置不会停留在ccp(0.35, 0.965),它只是重置为 0,0。

这里有什么我遗漏的吗?

【问题讨论】:

  • 尝试一些较大的位置值..
  • iPhone/iPad 无法将您的标签显示到 (0.35, 0.965) 像素。为什么不使用整数值?
  • 你可以使用float,但是(0.35, 0.965)大约是(0,0)...

标签: cocos2d-iphone cclayer cclabelttf ccnode


【解决方案1】:

问题是 CCPositionNormalized。如果你使用它,它不需要实际的像素大小。一旦我将其注释掉并使用实际像素大小(例如 ccp(100, 200) ),它就可以正常工作。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多