Cocos2d 2.0 and CCSpriteBatchNode: CCAnimation cra

2019-09-02 19:04发布

问题:

EDIT 4: I added a breakpoint to CCSpriteFrameCache spriteFrameByName method and cannot step over the following step (its clear to me that somehow the spriteFrames gets deleted from the CCSpriteFrameCache):

EDIT 3: Before voting to close the question actually PLEASE TRY the code below (EDIT2) using Cocos2d 2.0 and the file and plist file attached.

EDIT 2: I created a brand new GameScene test class so you can all try out the issue. It crashes as soon as the animation starts to repeat. I update the title as consequence as well.

//  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

The artfile.png looks as following:

And the plist file is:

<?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>

END OF EDIT2:

ORIGINAL QUESTION:

I get:

Assertion failure in -[CCSprite setTexture:], /Users/.../libs/cocos2d/CCSprite.m:934

It has definetely to do with the batchNode because without it it used to work...

I know that the sprite frames for the animation should be in the same sprite sheet, and that's the case. I have no idea how to solve this apart that it must be something to do with the use of batch node.

//
//  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;
}

EDIT: I checked the source code of CCSprite setTexture and here is what I see. However the console log does say only NSAssert without giving the log message of what is the problem. Adding a breakpoint it seems to fail the first NSAssert but I am not 100% sure as the App goes immediately to this 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];
    }
}

回答1:

Do you have only one .plist and .png file in your resources both with the -hd suffix? Are you sure that all the frame names are in your plist? Are you sure that you added your resources to your project correctly?

Also, usually you can add frames to an animation as follows:

         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];


回答2:

I found the reason why this was happening... I was calling the [self startGame] method from a previous scene and then the cleanup method was being called calling also the piece of code where I was removing sprites from the texture cache.

Stupid me that I didn't thought about this properly. The object Navigator scene gets created and the texture loaded, but in the meanwhile (Before the animation actually starts) the previous scene cleanup call executes the [CCSpriteFrameCache purgeSharedSpriteFrameCache]; call.

Stupid me!

-(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
}