سؤال

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

Cause all the 100 sprites used the same texture,so the best way is to use CCSpriteBatchNode. It will draw them in 1 single OpenGL call.

CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create("sprite.png", 100);
batchNode->setPosition(CCPointZero);
this->addChild(batchNode);

for(int i = 0;i < 100;++i)
{
   CCSprite* sprTest = CCSprite::createWithTexture(batchNode->getTexture());
   sprTest->setPosition(ccp(i*10,100));
   batchNode->addChild(sprTest);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top