1

我在为我的游戏创建按钮类时遇到问题。我的按钮图像在精灵表中,女巫是我在 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>
4

1 回答 1

1

嗯……我觉得还可以。可能找不到 .plist 。验证 .plist 是否是目标的成员,并且文件名大小写在资源目录中是否正确。

此外,在这一行:

[[self parent] addChild:buttonsNode];

不确定调用 init 方法时是否设置了 parent。

编辑:在您从 plist 添加精灵帧的行上放置一个断点。在 CCSpriteFrameCache 的代码中进入它,直到你在这里:

-(void) addSpriteFramesWithFile:(NSString*)plist
{
    NSAssert(plist, @"plist filename should not be nil");

    if( ! [loadedFilenames_ member:plist] ) {

        NSString *path = [[CCFileUtils sharedFileUtils] fullPathFromRelativePath:plist];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

        NSString *texturePath = nil;
....

并验证 dict 不为零。cocos2d 用于静默加载目录的方法“失败”,即如果文件不存在则返回 nil Dictionary 对象。如果文件存在,则问题是其他问题。如果 dict 为 nil,那么...继续查看您的项目设置,直到找到问题,或者使用文件命名(请注意项目中的文件名和 finder 中的文件名的大小写:必须相同),或者文件是不是您的目标的成员。此外,如果您为项目启用了高清,请确保 plist 是“Buttons-hd.plist”,与纹理 (Buttons-hd.png) 相同。同样,CCSpriteBatchNode 在缺少纹理的情况下“什么都不做”,静默失败。

于 2012-11-25T15:50:28.910 回答