Cocos2D 标签显示不出来,黑屏

Cocos2D 标签显示不出来,黑屏

问题描述:

实现CCLabelProtocolCCRGBAProtocol是在命名为Label的类中:

#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类中的。

但是运行之后屏幕是黑的,没显示label。

因为你只用了几个协议,继承CCNode。CCNode是不可见的

你可以让label继承 CCLabelTTF,由于 CCLabelTT已经实现了,可以省略协议,

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