Cocos2d 2.0和CCSpriteBatchNode:当CCRepeatForever启动第二个动画周期时,CCAnimation会崩溃APP

时间:2013-01-14 13:22:31

标签: ios animation cocos2d-iphone textures

编辑4:我在CCSpriteFrameCache spriteFrameByName方法中添加了一个断点,并且无法跳过以下步骤(我清楚地知道spriteFrames会从CCSpriteFrameCache中删除):

Step that cannot be stepped over

编辑3:在使用 Cocos2d 2.0以及附加的文件和plist文件进行投票以实际关闭问题之前请尝试下面的代码(EDIT2)。

编辑2:我创建了一个全新的GameScene测试类,所以你们都可以尝试这个问题。一旦动画开始重复,它就会崩溃。我也会更新标题。

//  GameScene2.h
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface GameScene2 : CCScene {

}
@end

//  GameScene2.m
//
#import "GameScene2.h"

@implementation GameScene2
-(id)init {
    self = [super init];
    if (self != nil) {
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"artfile.plist"];
        CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithFile:@"artfile.png"];
        [self addChild:batchNode];

        CCSprite * test = [CCSprite spriteWithSpriteFrameName:@"frame0.png"];
        test.anchorPoint = CGPointMake(0.5f, 0.5f);
        test.position = CGPointMake(160.0f, 160.0f);
        [batchNode addChild:test z:1];


        NSMutableArray* frames = [[NSMutableArray alloc]initWithCapacity:2];
        CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"frame0.png"];
        [frames addObject:frame];
        CCSpriteFrame* frame2  = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"frame1.png"];
        [frames addObject:frame2];

        CCAnimation* anim = [CCAnimation animationWithSpriteFrames:frames delay:0.3f];
        [[CCAnimationCache sharedAnimationCache] addAnimation:anim name:@"test"];
        CCAnimate * animate = [CCAnimate actionWithAnimation:anim];
        CCRepeatForever * repeat = [CCRepeatForever actionWithAction:animate];
        //CCSequence * seq = [CCSequence actions:anim, nil];

        [test runAction:repeat];
    }
    return self;
}
@end

artfile.png如下所示: artfile.png

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>frame0.png</key>
        <dict>
            <key>aliases</key>
            <array>

            </array>
            <key>spriteColorRect</key>
            <string>{{3, 40}, {76, 28}}</string>
            <key>spriteOffset</key>
            <string>{0, 0}</string>
            <key>spriteSize</key>
            <string>{150, 94}</string>
            <key>spriteSourceSize</key>
            <string>{150, 94}</string>
            <key>spriteTrimmed</key>
            <false/>
            <key>textureRect</key>
            <string>{{0, 96}, {150, 94}}</string>
            <key>textureRotated</key>
            <false/>
        </dict>

        <key>frame1.png</key>
        <dict>
            <key>aliases</key>
            <array>

            </array>
            <key>spriteColorRect</key>
            <string>{{10, 33}, {98, 32}}</string>
            <key>spriteOffset</key>
            <string>{0, 0}</string>
            <key>spriteSize</key>
            <string>{150, 94}</string>
            <key>spriteSourceSize</key>
            <string>{150, 94}</string>
            <key>spriteTrimmed</key>
            <false/>
            <key>textureRect</key>
            <string>{{0, 192}, {150, 94}}</string>
            <key>textureRotated</key>
            <false/>
        </dict>

        <key>frame2.png</key>
        <dict>
            <key>aliases</key>
            <array>

            </array>
            <key>spriteColorRect</key>
            <string>{{4, 17}, {146, 48}}</string>
            <key>spriteOffset</key>
            <string>{0, 0}</string>
            <key>spriteSize</key>
            <string>{150, 94}</string>
            <key>spriteSourceSize</key>
            <string>{150, 94}</string>
            <key>spriteTrimmed</key>
            <false/>
            <key>textureRect</key>
            <string>{{0, 0}, {150, 94}}</string>
            <key>textureRotated</key>
            <false/>
        </dict>

    </dict>
    <key>metadata</key>
    <dict>
        <key>version</key>
        <string>1.5.2</string>
        <key>format</key>
        <integer>3</integer>
        <key>size</key>
        <string>{1024, 1024}</string>
        <key>name</key>
        <string>artfile</string>
        <key>premultipliedAlpha</key>
        <false/>
        <key>target</key>
        <dict>
            <key>name</key>
            <string>default</string>
            <key>textureFileName</key>
            <string>artfile_default</string>
            <key>textureFileExtension</key>
            <string>.png</string>
            <key>coordinatesFileName</key>
            <string>artfile_default</string>
            <key>coordinatesFileExtension</key>
            <string>.plist</string>
            <key>premultipliedAlpha</key>
            <false/>
        </dict>
    </dict>
</dict>
</plist>

编辑结束2:

原始问题:

我明白了:

断言失败 - [CCSprite setTexture:],/ Users /.../ libs / cocos2d / CCSprite.m:934

它与batchNode有明确的关系,因为没有它它曾经工作...

我知道动画的精灵帧应该在同一个精灵表中,就是这种情况。我不知道如何解决这个问题,它必须与使用批处理节点有关。

//
//  Navigator.h
@interface Navigator : CCLayer {

}
//Batch Nodes
@property(readwrite, nonatomic) CCSpriteBatchNode* batchNode;


//  Navigator.m
@implementation Navigator
@synthesize batchNode;

+(id) scene {
    CCScene *scene = [CCScene node];
    Navigator *layer = [Navigator node];//??
    [scene addChild:layer];
    return scene;
}

-(void) loadStuff{
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"art1-hd.plist"];
    batchNode = [CCSpriteBatchNode batchNodeWithFile:@"art1-hd.png"];
    [self addChild:batchNode];

    CCSprite * test = [CCSprite spriteWithSpriteFrameName:@"emptyCircle0.png"];
    test.anchorPoint = CGPointMake(0.5f, 0.5f);
    test.position = CGPointMake(160.0f, 160.0f);
    [batchNode addChild:test z:1];


    NSMutableArray* frames = [[NSMutableArray alloc]initWithCapacity:2];
    CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"emptyCircle0.png"];
    [frames addObject:frame];
    CCSpriteFrame* frame2  = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"emptyCircle1.png"];
    [frames addObject:frame2];

    CCAnimation* anim = [CCAnimation animationWithSpriteFrames:frames delay:0.3f];
    //[[CCAnimationCache sharedAnimationCache] addAnimation:anim name:@"test"];
    CCAnimate * animate = [CCAnimate actionWithAnimation:anim];
    CCRepeatForever * repeat = [CCRepeatForever actionWithAction:animate];
    //CCSequence * seq = [CCSequence actions:anim, nil];

    [test runAction:repeat];
}


-(id)init
{
    CCLOG(@"Navigator init");
    if((self=[super init])){
        CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
        [self loadStuff];
        }
    return self;
}

编辑:我检查了CCSprite setTexture的源代码,这就是我所看到的。但是,控制台日志确实只说NSAssert而没有给出问题的日志消息。添加断点似乎无法通过第一个NSAssert,但我并不是100%肯定,因为应用程序立即转到此图像:

Image

-(void) setTexture:(CCTexture2D*)texture
{
    // If batchnode, then texture id should be the same
    NSAssert( !batchNode_ || texture.name == batchNode_.texture.name , @"CCSprite: Batched sprites should use the same texture as the batchnode");  

    // accept texture==nil as argument
    NSAssert( !texture || [texture isKindOfClass:[CCTexture2D class]], @"setTexture expects a CCTexture2D. Invalid argument");

    if( ! batchNode_ && texture_ != texture ) {
        [texture_ release];
        texture_ = [texture retain];

        [self updateBlendFunc];
    }
}

2 个答案:

答案 0 :(得分:0)

您的资源中是否只有一个带有-hd后缀的.plist和.png文件?你确定所有的帧名都在你的plist中吗?您确定已将资源正确添加到项目中吗?

此外,通常您可以按如下方式向动画添加帧:

         animFrames = [[NSMutableArray alloc] init];
         for(int i =1; i<7; i++){

              [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"anim-00%d.tif", i]]];
        }
         CCAnimation *animAnimation = [CCAnimation animationWithSpriteFrames:animFrames delay:0.04f];

答案 1 :(得分:0)

我找到了发生这种情况的原因......我从前一个场景中调用了[self startGame]方法,然后调用了cleanup方法,同时调用了我从纹理缓存中删除sprite的代码片段

愚蠢我没想到这个。创建对象导航器场景并加载纹理,但同时(在动画实际开始之前),前一场景清理调用执行 [CCSpriteFrameCache purgeSharedSpriteFrameCache]; 调用。

愚蠢的我!

-(void) startGame
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[Navigator scene] withColor:ccc3(255, 255, 255)]];
}

-(void) cleanup
{
//    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
    [super cleanup];


    [CCTextureCache purgeSharedTextureCache];
    [CCSpriteFrameCache purgeSharedSpriteFrameCache];
    [CCAnimationCache purgeSharedAnimationCache];

    //Remove music
}