【问题标题】:Cocos2D label not visible, black screenCocos2D标签不可见,黑屏
【发布时间】:2012-12-06 00:45:31
【问题描述】:

我已经使用我命名为 Label 的类实现了 CCLabelProtocol 和 CCRGBAProtocol:

#import <cocos2d.h>

@interface Label : CCNode <CCLabelProtocol, CCRGBAProtocol>

@property (nonatomic, copy, readwrite) NSString* string;
@property (nonatomic, readwrite) ccColor3B color;
@property (nonatomic, readwrite) GLubyte opacity;

- (id) initWithString: (NSString*) aString;

@end

由于它好用,我还做了一个 initWithString 方法:

#import "Label.h"

@implementation Label

@synthesize string,opacity,color;

- (id) initWithString: (NSString*) aString
{
    if(self=[super init])
    {
        string= aString;
        color.r=255;
        color.g=0;
        color.b=0;
        opacity= 10;
    }
    return self;
}

- (ccColor3B) color
{
    return color;
}

@end

然后在 HelloWorldLayer 的 init 方法中我这样做:

    Label* label= [[Label alloc]initWithString: @"Start"];
    CCMenuItemLabel* item= [CCMenuItemLabel itemWithLabel: label block:^(id sender)
    {
        NSLog(@"Label Clicked");
    }];
    CCMenu* menu= [CCMenu menuWithItems: item, nil];
    [self addChild: menu];

我使用的是普通的 cocos2d 模板,所以这是在 CCLayer 类中。
屏幕是黑色的,我没有得到我想要的标签。

【问题讨论】:

    标签: iphone objective-c ios cocos2d-iphone xcode4.5


    【解决方案1】:

    那是因为你只是采用了一些协议,而是从 CCNode 继承而来的。 CCNode 是一个非渲染(不可见)节点。

    如果你想要一个标签,从 CCLabelTTF 子类化并省略协议,因为 CCLabelTTF 已经实现了它们。

    但是,除非您想更改标签的行为或呈现方式,否则创建 CCLabelTTF 子类是多余的。如果您只需要更改文本和颜色,请使用 CCLabelTTF initializers 及其颜色属性。

    Label* label = [CCLabelTTF labelWithString:@"Start" fontName:@"Arial" fontSize:24];
    label.color = ccRED;
    CCMenuItemLabel* item = [CCMenuItemLabel itemWithLabel: label block:^(id sender)
    {
        NSLog(@"Label Clicked");
    }];
    CCMenu* menu = [CCMenu menuWithItems:item, nil];
    [self addChild:menu];
    

    【讨论】:

      猜你喜欢
      • 2021-03-08
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多