我在为我的游戏创建按钮类时遇到问题。我的按钮图像在精灵表中,女巫是我在 Zwoptex 中创建的。问题是程序在加载精灵时崩溃。
这是Buttons.h:
#import "CommonProtocols.h"
#import "GameObject.h"
@interface Buttons : CCSprite
{
CCSprite *buttonSprite;
ButtonTypes *buttonType;
CCSpriteBatchNode *buttonsNode;
}
+(id)button;
-(id)initButtonWithType:(ButtonTypes)bType;
@property (readwrite,assign)CCSpriteBatchNode* buttonsNode;
@property (readwrite,assign)CCSprite *buttonSprite;
@property (readonly)ButtonTypes *buttonType;
@end
这是实现:
#import "Buttons.h"
@implementation Buttons
@synthesize buttonSprite;
@synthesize buttonsNode;
@synthesize buttonType;
-(void)dealloc
{
[buttonSprite release];
[super dealloc];
}
+(id)button
{
return [[[self alloc] init] autorelease];
}
-(id)initButtonWithType:(ButtonTypes)btype
{
if(self=[super init]){
buttonsNode = [CCSpriteBatchNode batchNodeWithFile:@"Buttons.png"];
[[self parent] addChild:buttonsNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Buttons.plist"];
switch (btype)
{
case kFireButton:
[self setButtonSprite:[CCSprite spriteWithSpriteFrameName:@"firebutton.png"]];
[buttonsNode addChild:[self buttonSprite]];
return self;
break;
case kLeftButton:
[self setButtonSprite:[CCSprite spriteWithSpriteFrameName:@"leftbutton.png"]];
[buttonsNode addChild:[self buttonSprite]];
return self;
break;
case kRightButton:
[self setButtonSprite:[CCSprite spriteWithSpriteFrameName:@"rightbutton.png"]];
[buttonsNode addChild:[self buttonSprite]];
return self;
break;
case kReplayButton:
[self setButtonSprite:[CCSprite spriteWithSpriteFrameName:@"replaybutton.png"]];
[buttonsNode addChild:[self buttonSprite]];
return self;
break;
default:
NSLog(@"NO button");
break;
}
}
return self;
}
@end
当我在其他地方调用此方法时程序崩溃:
Buttons *myButton = [[Buttons button] initButtonWithType:kFireButton];
[self addChild:myButton];
这是程序输出的内容:
cocos2d: CCSpriteFrameCache: Frame 'firebutton.png' not found
*** Assertion failure in +[CCSprite spriteWithSpriteFrameName:],
最后是 Zwoptex 生成的 plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>frames</key>
<dict>
<key>firebutton.png</key>
<dict>
<key>aliases</key>
<array>
</array>
<key>spriteColorRect</key>
<string>{{1, 2}, {38, 40}}</string>
<key>spriteOffset</key>
<string>{0, 2}</string>
<key>spriteSize</key>
<string>{38, 40}</string>
<key>spriteSourceSize</key>
<string>{40, 48}</string>
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
<string>{{51, 1}, {38, 40}}</string>
<key>textureRotated</key>
<false/>
</dict>
<key>leftbutton.png</key>
<dict>
<key>aliases</key>
<array>
</array>
<key>spriteColorRect</key>
<string>{{1, 0}, {46, 47}}</string>
<key>spriteOffset</key>
<string>{0, -0}</string>
<key>spriteSize</key>
<string>{46, 47}</string>
<key>spriteSourceSize</key>
<string>{48, 47}</string>
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
<string>{{1, 52}, {46, 47}}</string>
<key>textureRotated</key>
<false/>
</dict>
<key>replaybutton.png</key>
<dict>
<key>aliases</key>
<array>
</array>
<key>spriteColorRect</key>
<string>{{1, 0}, {46, 47}}</string>
<key>spriteOffset</key>
<string>{0, -0}</string>
<key>spriteSize</key>
<string>{46, 47}</string>
<key>spriteSourceSize</key>
<string>{48, 47}</string>
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
<string>{{49, 52}, {46, 47}}</string>
<key>textureRotated</key>
<false/>
</dict>
<key>rightbutton.png</key>
<dict>
<key>aliases</key>
<array>
</array>
<key>spriteColorRect</key>
<string>{{1, 0}, {46, 47}}</string>
<key>spriteOffset</key>
<string>{0, -0}</string>
<key>spriteSize</key>
<string>{46, 47}</string>
<key>spriteSourceSize</key>
<string>{48, 47}</string>
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
<string>{{1, 1}, {48, 49}}</string>
<key>textureRotated</key>
<false/>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>version</key>
<string>1.5.5</string>
<key>format</key>
<integer>3</integer>
<key>size</key>
<string>{128, 128}</string>
<key>name</key>
<string>Buttons</string>
<key>premultipliedAlpha</key>
<false/>
<key>target</key>
<dict>
<key>name</key>
<string>default</string>
<key>textureFileName</key>
<string>Buttons</string>
<key>textureFileExtension</key>
<string>.png</string>
<key>coordinatesFileName</key>
<string>Buttons</string>
<key>coordinatesFileExtension</key>
<string>.plist</string>
<key>premultipliedAlpha</key>
<false/>
</dict>
</dict>
</dict>
</plist>