Assertion failed: CCSpriteBatchNode only supports Sprites as children

Hello fellow developers.
Working on a game that involves a timer. I did some research and was happy to find ProgressTimer.
Thing is my game crashes due to an assertion failure: CCSpriteBatchNode only supports Sprites as children.
I’m using version 3.16 and developing using cpp.

See code below and more details.
*.h
ProgressTimer* _cardTimer;

*.cpp
_cardTimer = ProgressTimer::create(Sprite::createWithSpriteFrameName("timerSprite.png"));
_cardTimer->setPosition(Vec2(_screenSize.width * 0.5f, _screenSize.height * 0.6879f));
_cardTimer->setType(ProgressTimer::Type::RADIAL);
_cardTimer->setPercentage(0.0f);
_spriteBatchNode->addChild(_cardTimer, kzForeground);

Assertion happens when program reaches the statement: _spriteBatchNode->addChild(_cardTimer, kzForeground);
Exact fail location is CCSpriteBatchNode.cpp:192

Am I missing something here or is it actually a bug??
Will pay with cat emojis for help :smiley_cat:

This is actually the reason :wink:

4 Likes

@dimon4eg
This is actually the reason :wink:

Hmmm I see what you mean…
I had the impression that ProgressTimer inherits from Sprite (it requires a Sprite on create).
So how do I actually add the ProgressTimer to the scene???!?!

Add it to other cocos2d node e.g. Layer or even just Node.
What is parent of your _spriteBatchNode?

Just because it uses a sprite doesn’t mean it inherits from it. If you check the declaration of ProgressTimer, you would see that it inherits from Node, not Sprite:

class CC_DLL ProgressTimer : public Node

If you want more info, read up on inheritance.

4 Likes

Yup, it’s the Layer node.
_cardTimer = ProgressTimer::create(Sprite::create("timerSprite.png"));
_cardTimer->setPosition(Vec2(_screenSize.width * 0.5f, _screenSize.height * 0.6879f));
_cardTimer->setType(ProgressTimer::Type::RADIAL);
_cardTimer->setPercentage(0.0f);
this->addChild(_cardTimer, kzForeground);
Made the change to so addChild would refer to the Layer node and it worked!
Thanks a lot for that :smiley_cat::smiley_cat::smiley_cat: !!

This is definitely an advanced concept in Cocos2d, dealing with non SpriteFrameCache nodes. It also introduces the challenge of ordering the nodes (z-order wise). But that’s for another topic. :hugs:

For z-order suggestion: do not use setLocalZOrder or setGlobalZOrder.
Just add your nodes to correct parent node as your child will be on top of parent.

Yup, re-arranging the nodes wouldn’t really do something.
Lucky my scene has just a few sprites so I’ve managed to fix it pretty easily.
Thanks again for the help :smiley_cat:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.