oOOMIAO 2012-12-06 01:54 采纳率: 10%
浏览 2457
已采纳

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。

  • 写回答

1条回答

  • g989_1314125 2012-12-06 02:58
    关注

    因为你只用了几个协议,继承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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题