开发者社区 问答 正文

Cocos2D 标签显示不出来,黑屏

实现CCLabelProtocol和CCRGBAProtocol是在命名为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。

展开
收起
爵霸 2016-05-27 10:40:58 1876 分享 版权
1 条回答
写回答
取消 提交回答
  • 因为你只用了几个协议,继承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];
    2019-07-17 19:17:16
    赞同 展开评论
问答地址: