Real sprite type?

So, i was wanting to make an array of sprites, so i could use an action on all of them instead of just one. But, to do this I’d need the actual type of a sprite, instead of just using auto. So, please help. Thanks!

Post your code. I do this all the time.

Use the

auto sprite = spriteCreate(“WhayEver”);

You can uses Tag function


all them sprite are now assinged 999

for ( auto &Item : Director::getInstance()->getRunningScene()->getChildren() )
        if(Item->getTag() == 999) {

There is also a setName(0 and getName();
change it to getChildByName();

but this is awkward because then you can’t use getTag() to get a unique sprite if you need it.

why not just store the Sprites in an array or vector and iterate through it and run the actions you need on it. Your solution affects every node on the scene.

Oh Ok,

auto node = Node::create()

for(int i=0; i < 10; i++ ) {
auto sprite = spriteCreate(“WhayEver1.png”);


for ( auto &Item : node->getChildren() )
if(Item->getTag() == 999) {

it’s off the root node now.

The type of a sprite is cocos2d::Sprite, which inherits from cocos2d::Node. If I’m understanding the question correctly :slightly_smiling_face:

An array of sprites for example:

cocos2d::Sprite* spriteArray[256];

But I think OP has Sprites already and he wants to loop through the sprites and perform logic on them (under certain conditions maybe… OP, clarify?)

add your sprite to the vector upon creation:

CornSprite* corn = CornSprite::createSprite(``````);
addChild(corn, 1); // add to the scene or not yet, your call
_cornSprites.push_back(corn); // add to a vector for later

// do some more logic on your sprite if you wish
corn->setPosition(cocos2d::Point(getContentSize().width / 2, 
            getContentSize().height / 2));

later do stuff to the sprites in your vector

for(auto& sprite : _cornSprites)
    if( // some logic)
        // do something....

I suppose there are many ways to do it.

auto doesn’t change the type, and simply deduces the type at compile time, so for example, auto obj = Sprite::create(...) will result in obj being of type cocos2d::Sprite*. The only way things won’t work is if your code explicitly converts Sprite* to something else, but if you do that, then your code is also responsible for converting it back to Sprite*, and auto has nothing to do with it.

Do you meen you want to know if its a sprite or cast it to somting else
Node* node= static_cast<Sprite*>(obj);
Sprite* sprite = static_cast<Node*>(obj);

becase I thught was about adding opration to a sprite if its the right one.