简体   繁体   English

cocos2d-iphone sprite工作表加载

[英]cocos2d-iphone sprite sheet loading

I have a problem while creating a button class for my game. 为游戏创建按钮类时遇到问题。 My button images are in a sprite sheet , witch I created in Zwoptex. 我的按钮图像在Sprite表中,这是我在Zwoptex中创建的。 The problem is that the program crashes while loading the sprites. 问题是程序在加载精灵时崩溃。

Here is Buttons.h: 这是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

And here is the implementation: 这是实现:

#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

The program crashes when I elsewhere call this methods: 当我在其他地方调用此方法时,程序崩溃:

 Buttons *myButton = [[Buttons button] initButtonWithType:kFireButton];
        [self addChild:myButton];

And here is what the program outputs: 程序输出如下:

cocos2d: CCSpriteFrameCache: Frame 'firebutton.png' not found

*** Assertion failure in +[CCSprite spriteWithSpriteFrameName:],

And finally here is the plist that Zwoptex generated: 最后是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>

Hmmm ... looks ok to me. 嗯...对我来说还可以。 Probably not finding the .plist . 可能找不到.plist。 Verify that the .plist is a member of your target, and that the filename case is correct in your resources directory. 验证.plist是目标的成员,并且资源目录中的文件名大小写正确。

Also, on this line : 另外,在这一行:

[[self parent] addChild:buttonsNode];

not certain that parent is set when you call the init method. 不确定在调用init方法时是否设置了parent。

Edit : put a breakpoint on the line where you add sprite frames from the plist. 编辑:在您从plist添加子画面帧的行上放置一个断点。 Step into it until you are HERE , in CCSpriteFrameCache's code : 进入它,直到使用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;
....

and verify that dict is not nil. 并确认dict不为零。 The method used by cocos2d for loading a directory silently 'fails', ie returns a nil Dictionary object if the file does not exist. cocos2d用于静默加载目录的方法“失败”,即,如果文件不存在,则返回nil Dictionary对象。 If the file exists the problem is something else. 如果文件存在,则问题出在其他方面。 If dict is nil, well ... keep looking in your project setup until you find the issue, either with file naming (be mindful of the case for the filename in project and in finder : MUST be the same), or the file is not a member of your target. 如果dict为零,那么...继续查找项目设置,直到找到问题为止,要么使用文件命名(请注意项目中和finder中文件名的大小写:必须相同),或者文件是不是您目标的成员。 Also, if you enabled HD for your project, make certain that the plist is "Buttons-hd.plist", same for the texture (Buttons-hd.png). 另外,如果为项目启用了HD,请确保plist为“ Buttons-hd.plist”,与纹理(Buttons-hd.png)相同。 Same, CCSpriteBatchNode does 'nothing' with a missing texture, fails silently. 同样,CCSpriteBatchNode在缺少纹理的情况下不执行任何操作,但会自动失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM