我正在使用Cocosbutton类来创建一个添加到场景中的按钮。这将创建一个 CCSprite,它被添加到 CCMenu 内的 CCMenuItem 中:
@interface CocosButtonItem : CCMenuItem {
...
在init方法中,添加了背景:
UIImage *bgImage = [UIImage imageWithContentsOfFile:@"buttonback.png"],
back = [CCSprite spriteWithCGImage:bgImage.CGImage key:@"bgimage"];
back.anchorPoint = ccp(0,0);
self.contentSize = back.contentSize;
CCLayerColor *bgFill = [CCLayerColor layerWithColor:ccc4(255.0, 255.0, 255.0, 255.0)];
bgFill.contentSize = self.contentSize;
bgFill.position = ccp(0,0);
[self addChild:bgFill];
back.color = ccc3(255,255,255);
[self addChild:back]; // without this, I just get a whit layer (as intended)
添加最后一行以将精灵添加到 CCMenuItem 后,我会得到黑色背景(见屏幕截图)。CCSprite 背景图像的来源是具有透明度的 8 位 .png,我无法弄清楚为什么我会看到这个黑色背景。