What is the performatic way of switching CCSpriteBatchNode?

68 Views Asked by At

I need to switch texture atlas from CCSpriteBatch node. The approach I chose is, after the playable character reaches a specific X axis (700) the game changes it.

and it works fine visually, but the problem is, it freezes for 2-3 seconds the screen while changing and I receive memory warning in the console.

I don't think I am the only one who ever did that, which makes me wonder how is the proper(lightest) way of switching CCSpriteBatchNode.

See below the current switching code:

if(mainCharacter.x > 700)
{
    //NSLog(@"switch!");

    float oldHeroX, oldHeroY;

    oldHeroX = mainCharacter.x;
    oldHeroY = mainCharacter.y;

    [_myBatchNode removeChild:mainCharacter];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:texturePList1];
    [self removeChild:_myBatchNode];



    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:texturePList2];
    _myBatchNode = [CCSpriteBatchNode batchNodeWithFile:texturePNG2];
    [_myBatchNode.texture setAliasTexParameters];
    [self addChild: _myBatchNode z:-6];

    //this is just a method that starts the playable character sprite and add to batch node and layer
    [self startCharacter:oldHeroX :oldHeroY];
}

thanks

1

There are 1 best solutions below

0
On

I tend to stay away from loading 'on the fly', by and large : i.e. i preload all my textures before starting a scene (or cut-scene), typically during the transition. When stuck, and i must load on the fly (because the memory footprint of preloaded textures is too high), i will use pvr textures (uncompressed), sacrificing bundle size to gain load speed and memory usage during the load. YMMV, depending on the size of the individual textures you need to switch 'on the fly'.

Also, if all you need is either a sprite or an animation, dont use batch node. Nothing gained, but the cleanup and wind-down is costly.