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;
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
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???!?!
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:
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 !!
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.
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