CCLabelTTF的构造函数在cocos2d v3.0中

时间:2014-04-16 10:09:09

标签: ios cocos2d-iphone cclabelttf

我读了这个document并声明:

CCLabelTTF *label = [CCLabelTTF initWithString: @"a" fontName: @"HelveticaNeue" fontSize: 24 dimensions: CGSizeMake(320,480) hAlignment: CCVerticalTextAlignmentTop];

但它的显示警告:没有知道选择器的类方法... 有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:1)

initWithString ...不是静态方法,所以你需要先分配CCLabelTTF:

CCLabelTTF *label = [[CCLabelTTF alloc] initWithString: @"a" fontName: @"HelveticaNeue" fontSize: 24 dimensions: CGSizeMake(320,480) hAlignment: CCVerticalTextAlignmentTop];

或者您可以使用静态方法:

CCLabelTTF *label = [CCLabelTTF labelWithString: @"a" fontName: @"HelveticaNeue" fontSize: 24 dimensions: CGSizeMake(320,480) hAlignment: CCVerticalTextAlignmentTop];

答案 1 :(得分:1)

在Cocos2d V3中,cclabelTTF已更改。 CCLabelTTF.m中的可用方法仅为:

+ (id) labelWithString:(NSString *)string fontName:(NSString *)name fontSize:    (CGFloat)size
 {
   return [[self alloc] initWithString:string fontName:name fontSize:size];
 }

 + (id) labelWithString:(NSString *)string fontName:(NSString *)name fontSize:(CGFloat)size dimensions:(CGSize)dimensions
 {
     return [[self alloc] initWithString:string fontName:name fontSize:size dimensions:dimensions];
 }

+ (id) labelWithAttributedString:(NSAttributedString *)attrString
{
    return [[self alloc] initWithAttributedString:attrString];
}

 + (id) labelWithAttributedString:(NSAttributedString *)attrString dimensions:(CGSize)dimensions
 {
     return [[self alloc] initWithAttributedString:attrString dimensions:dimensions];
 }

所以在COCOS2d V3中你的实现是不正确的。这对V2版本有效。我认为仍然需要更新文档。

您应该使用以上四种方法中的任何一种,例如:

CCLabelTTF *label = [CCLabelTTF labelWithString@"a" fontname:@"Arial" fontsize:12 dimension:(CGSize)dimensions];
   label.verticalAlignment = ******
 or
   label.horizontalAlignment = ****

这将在内部调用构造函数,如上所述。