Search code examples
cocos2d-x

cocos2d-x Why i can't set sprite hierarchy when using SpriteBatchNode?


Hey i try to set sprite hierarchy to parent root , taking the example from SpriteTest.cpp from SpriteSkewNegativeScaleChildren example.
but in my code i allso add the sprites to SpriteBatchNode .
like this :

auto cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("leds/sprites.plist", "leds/sprites.png");   
    auto batch = SpriteBatchNode::create("leds/sprites.png");
    Sprite* Sprite_all_side_connector = Sprite::createWithSpriteFrameName("all_side_connector.png");
    batch->addChild(Sprite_all_side_connector);
    Sprite* Sprite_one_side_connector = Sprite::createWithSpriteFrameName("one_side_connector.png");
    batch->addChild(Sprite_one_side_connector);
    Sprite* Sprite_purple_stick = Sprite::createWithSpriteFrameName("purple_stick.png");
    batch->addChild(Sprite_purple_stick);
    Sprite* Sprite_red_stick = Sprite::createWithSpriteFrameName("red_stick.png");
    batch->addChild(Sprite_red_stick);
    Sprite* Sprite_yellow_ball = Sprite::createWithSpriteFrameName("yellow_ball.png");
    batch->addChild(Sprite_yellow_ball);

     addChild(batch, 0, TAGS::SPRITEBATCHNODE);        
    Sprite_all_side_connector->setPosition(ccp(winSize.width/2,winSize.height/2));
    auto parent = Node::create();
    addChild(parent);
    parent->addChild(Sprite_all_side_connector); 

but im getting exception , when i remove the SpriteBatchNode every thing working fine .
i want to use the SpriteBatchNode feature for OpenGL one draw call .

the exception is in :

parent->addChild(Sprite_all_side_connector); 

The compiler error


Solution

  • You will need to create another instance of Sprite_all_side_connector and add that to the scene. Sprites can only be added once. Change your code to:

    parent->addChild(Sprite::createWithSpriteFrameName("all_side_connector.png"));

    That should clean up the assertion.