这一行中的断言失败

时间:2011-03-08 09:49:11

标签: iphone cocos2d-iphone sprite

CCSprite *ni = [CCSprite spriteWithFile:@"blue_animation.png"];
ni.position = ccp(5,66);

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
    @"blue_ani.plist"];

CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode 
                                  batchNodeWithFile:@"blue_ani.png"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 3; ++i) {
    [walkAnimFrames addObject:
        [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
        [NSString stringWithFormat:@"baloon-bl_%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation 
                         animationWithFrames:walkAnimFrames delay:0.1f];

CCSprite *blue_action_1 = [CCSprite spriteWithSpriteFrameName:@"blue_ani.png"];        
CGSize winSize = [CCDirector sharedDirector].winSize;

blue_action_1.position = ccp(winSize.width/2, winSize.height/2);
self.blue_action_motion = [CCRepeatForever actionWithAction:
                          [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[blue_action_1 runAction:blue_action_motion];
[spriteSheet addChild:blue_action_1]; 

cocso2d中-[CCSprite initWithSpriteFrame:]的断言失败

我该如何解决呢。

3 个答案:

答案 0 :(得分:1)

尝试替换此行:

CCSprite *blue_action_1 = [CCSprite spriteWithSpriteFrameName:@"blue_ani.png"];

这一行:

CCSprite *blue_action_1 = [CCSprite spriteWithSpriteFrameName:@"baloon-bl_1.png"];

基本上你的“blue_ani.png”是一个包含多个精灵的spritesheet,因此你不能将整个spritesheet用作你的精灵。所以你需要通过提供一个有效的框架名称(“baloon-bl_1.png”,“baloon-bl_2.png”或“baloon-bl_3.png”如果我推断出来)使用其中一个精灵来初始化你的blue_action_1精灵正确地从你的代码)。

答案 1 :(得分:0)

这是我的代码

CCSprite *ni = [CCSprite spriteWithFile:@"blue_animation.png"];
            ni.position = ccp(5,66);

            [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
             @"blue_ani.plist"];

            CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode 
                                              batchNodeWithFile:@"blue_ani.png"];
            [self addChild:spriteSheet];
            NSMutableArray *walkAnimFrames = [NSMutableArray array];
            for(int i = 1; i <= 3; ++i) {
                [walkAnimFrames addObject:
                 [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
                  [NSString stringWithFormat:@"baloon-bl_%d.png", i]]];
            }
            CCAnimation *walkAnim = [CCAnimation 
                                     animationWithFrames:walkAnimFrames delay:0.1f];

            CCSprite *blue_action_1 = [CCSprite spriteWithSpriteFrameName:@"blue_ani.png"];        
            CGSize winSize = [CCDirector sharedDirector].winSize;

            blue_action_1.position = ccp(winSize.width/2, winSize.height/2);
            self.blue_action_motion = [CCRepeatForever actionWithAction:
                               [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
            [blue_action_1 runAction:blue_action_motion];
            [spriteSheet addChild:blue_action_1];

答案 2 :(得分:0)

你是否将你的CCSprite * ni添加为孩子?