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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码