0
votes

In my app I have a great number of different animations of my sprite, and there will be more of tham in later versions. It is quite difficult to use texture file will hundreds of frames, so I whould like to make several of tham. So, I wrote this code:

    if (running)
    {
        NSArray * animations = [[physicalBody getAnimationList] objectForKey:ANIMATION_RUN];
        if (!inAir)
        {
            currentFrame++;
            if (currentFrame>=[animations count]*ANIMATION_ITERATION) 
                currentFrame = 0;
        }
        else
        {
            currentFrame = 2;
        }
        PhysicsSprite *sprite = (PhysicsSprite*)[physicalBody getSprite];
        [sprite setFlipX:(moveingDirection)];
        [sprite setDisplayFrame:[animations objectAtIndex:currentFrame/ANIMATION_ITERATION]];
    }
    else
    {
        NSArray * animations = [[physicalBody getAnimationList] objectForKey:ANIMATION_STAND];
        currentFrame++;
        if (currentFrame>=[animations count]*ANIMATION_ITERATION) 
            currentFrame = 0;
        PhysicsSprite *sprite = (PhysicsSprite*)[physicalBody getSprite];
        [sprite setFlipX:(moveingDirection)];
        [sprite setDisplayFrame:[animations objectAtIndex:currentFrame/ANIMATION_ITERATION]];   //error here
    }

Array of animations was created from different *.png, and when it tries to switch tham, i got this error:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteBatchNode'

How can I switch texture files? Or may be there is another solution?

1

1 Answers

0
votes

You can't change the texture a CCSpriteBatchNode uses.

However you can and should create a CCSpriteBatchNode for each texture that you are going to use. Then, whenever you set a display frame first check the frame's texture and use that to remove the sprite from the current sprite batch node and add it to the one that is setup to use the same texture as the sprite frame.

However this can lead to z ordering issues. In the worst case you'll simply have to either redesign your animations or don't use sprite batch nodes.