cocos2d-iphonecocos2d-xccspriteccspritebatchnode

Cocos2d-x: How to optimize memory from 100 same sprites?


My task is to draw one sprite 100 times in frame. For example I need to draw a row made of one sprite "sprite.png". I do it like this:

CCSprite *spriteArr[ 100 ];

for ( unsigned int i = 0; i < 100; i++ ) {

    spriteArr[ i ] = new cocos2d::CCSprite();
    spriteArr[ i ]->initWithFile( "sprite.png" );
    spriteArr[ i ]->setPosition( cocos2d::CCPoint( i * 10, 100 ) );

    this->addChild( spriteArr[ i ] );

}

And that's the problem. I allocate memory 100 times just only for one sprite but I don't know how to do it differently. How can I optimize it? Is there a way in Cocos2d for drawing a sprite using coordinates (x and y) but not to allocate memory for each same sprite?


Solution

  • You're good. All 100 sprites will reference the same texture object, so it's just a single texture in memory. Each sprite instance adds less than 500 bytes of memory on top of that.

    Your best option to conserve memory is to use the .pvr.ccz format for images.