Search code examples
cocos2d-x

Is it more efficient to draw a circle with ccCircle?


I'm hoping to create about 50-80 'balls' on the screen and each ball is an object of a class extending CCNode. Each ball will be about 20 x 20px.

Would it be more efficient to

  • override void draw() and use ccDrawCircle()
  • or make a picture and make a CCSprite with it (it'll needed be added to the node)?

Solution

  • Most effective way to draw multiple nodes, that use single material (texture) is CCSpriteBatchNode.

    // we create base bach node with texture
    CCSpriteBatchNode *batch = CCSpriteBatchNode::create(textureFile, 50 /* initial capacity of node */);
    
    // then we create our sprites with the same texture
    CCSprite::sprite = CCSprite::create(textureFile);
    batch->addChild(sprite);
    

    CCSpriteBatchNode can contain only CCSprites with the same texture, that used for batch node, but all sprites are drawed in single function call (batch drawing).

    Color and opacity of every sprite can be changed. To colorize sprites you can create white-colored texture of ball. When you use setColor method, white color will be transformed to color, that specified in setColor