Cocos2d应用程序崩溃,spriteFrameName无效

时间:2013-02-11 18:57:53

标签: iphone cocos2d-iphone sprite

我正在尝试使用cocos2d开发iphone游戏,我是初学者。 我试图在屏幕的上边界显示生命数量时遇到一些问题。所以我想从3个生命开始,并在每个错误中减少其中一个。这是我的代码

-(void)life {
    NSMutableArray *position = [NSMutableArray
                                arrayWithObjects:@"460", @"440", @"420", nil];
    spriteLifeArray = [[NSMutableArray alloc] init];
    for (int i = 0; i<life; i++) {
        spriteLife = [CCSprite spriteWithSpriteFrameName:@"cuore.png"];
        [spriteLife setTag:i];
        spriteLife.scale = 0.5;
        spriteLife.position = ccp([[position objectAtIndex:i] floatValue], 305);
        [self addChild:spriteLife];
        [spriteLifeArray addObject:spriteLife];
    }
}

这是我的游戏功能和删除生命的功能

-(void)gameOver:(int)value punteggio:(id)punti{
    if (value == 1) {
        // partita vinta
    } else if(value == 2) {
        if (life > 1) { // 1
            life = life - 1;
            for (CCSprite *spr in spriteLifeArray) {
                if (life == spr.tag) {
                    [self removeChild:spr cleanup:YES];
                    [self actionBlink];
                }
            }
        } else { 
            //  partita persa
        }
    }
}

资源文件夹中存在名为“cuore.png”的文件,但是当我尝试编译时,在启动画面后应用程序崩溃。这是日志:

  

2013-02-11 19:55:06.635 QuizGame [6672:c07] cocos2d:cocos2d v1.0.1   2013-02-11 19:55:06.636 QuizGame [6672:c07] cocos2d:使用Director   类型:CCDirectorDisplayLink 2013-02-11 19:55:06.664 QuizGame [6672:c07]   cocos2d:操作系统版本:6.1(0x06010000)2013-02-11 19:55:06.664   QuizGame [6672:c07] cocos2d:GL_VENDOR:Apple Computer,Inc。   2013-02-11 19:55:06.665 QuizGame [6672:c07] cocos2d:GL_RENDERER:Apple   软件渲染器2013-02-11 19:55:06.666 QuizGame [6672:c07] cocos2d:   GL_VERSION:OpenGL ES-CM 1.1 APPLE 2013-02-11 19:55:06.667   QuizGame [6672:c07] cocos2d:GL_MAX_TEXTURE_SIZE:4096 2013-02-11   19:55:06.667 QuizGame [6672:c07] cocos2d:GL_MAX_MODELVIEW_STACK_DEPTH:   16 2013-02-11 19:55:06.667 QuizGame [6672:c07] cocos2d:GL_MAX_SAMPLES:   4 2013-02-11 19:55:06.668 QuizGame [6672:c07] cocos2d:GL支持   PVRTC:是2013-02-11 19:55:06.668 QuizGame [6672:c07] cocos2d:GL   支持BGRA8888纹理:是2013-02-11 19:55:06.669   QuizGame [6672:c07] cocos2d:GL支持NPOT纹理:是2013-02-11   19:55:06.670 QuizGame [6672:c07] cocos2d:GL支持   discard_framebuffer:YES 2013-02-11 19:55:06.670 QuizGame [6672:c07]   cocos2d:使用NPOT支持编译:NO 2013-02-11 19:55:06.671   QuizGame [6672:c07] cocos2d:在TextureAtlas中使用VBO支持编译   :是2013-02-11 19:55:06.671 QuizGame [6672:c07] cocos2d:已编译   在CCNode中使用仿射矩阵变换:是2013-02-11   19:55:06.672 QuizGame [6672:c07] cocos2d:使用Profiling编译   支持:NO 2013-02-11 19:55:06.695 QuizGame [6672:c07] cocos2d:   CCSpriteFrameCache:找不到框架'cuore.png'2013-02-11   19:55:06.695 QuizGame [6672:c07] *断言失败+ [CCSprite   spriteWithSpriteFrameName:]   /Users/alexbava/Desktop/QuizGame/QuizGame/libs/cocos2d/CCSprite.m:110   2013-02-11 19:55:06.696 QuizGame [6672:c07] 终止应用程序   未捕获的异常'NSInternalInconsistencyException',原因:   '无效的spriteFrameName:cuore.png'   * *第一次调用堆栈:(0x2074012 0x1ca4e7e 0x2073e78 0x14e7665 0x6564e 0xc2bd7 0xc3229 0x40261 0xc1c4d 0xc12ec 0x9931af 0x9937a1   0x99494b 0x9a5cb5 0x9a6beb 0x998698 0x2d7cdf9 0x2d7cad0 0x1fe9bf5   0x1fe9962 0x201abb6 0x2019f44 0x2019e1b 0x99417a 0x995ffc 0xc0dcf   0x2665)libc ++ abi.dylib:terminate调用抛出异常(lldb)

我想我犯了一个愚蠢的错误或遗漏了什么。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:2)

您使用的CCSprite构造函数将在CCSpriteFrame缓存中查找CCSpriteFrame对象。如果你想要从名为'cuore.png'的文件中创建一个sprite,请使用构造函数:

CCSprite * spriteLife = [CCSprite spriteWithFile:@"cuore.png"];

精灵帧通常用于动画,或管理包含许多(有时不相关)纹理的大纹理地图集。

答案 1 :(得分:1)

spriteWithSpriteFrameName通常与CCSpriteFrameCache一起使用,请使用'spriteWithFile'。 :)