Reuse a sprite?

Hi !

How can i make this work:

    Sprite* aatroxQ = Sprite::createWithSpriteFrameName("Champ Skills/Aatrox_Q.png");
    Abilities.pushBack(aatroxQ);
    
    Sprite* braumR = Sprite::createWithSpriteFrameName("Champ Skills/Braum_R.png");
    Abilities.pushBack(braumR);
//********************************
************************//
Sprite* QuizGraphic::getRandQuiz()
{
    if (!Abilities.empty())
    {
        return Abilities.getRandomObject();
    }
    return NULL;
}

Then i use it in a for loop:

 for (int l = 1; l<4; l++) {
            Sprite* littleBoxd= QuizGraphic::getRandQuiz);
        littleBoxd->setAnchorPoint(Vec2(0.5, 1.0));
        float widthSpace = visibleSize.width/8;
            float heightSpace = visibleSize.height/8;
            if (l == 1) {
                littleBoxd->setPosition(Vec2( origin.x  + widthSpace*1.5, origin.y + heightSpace*i));
            }
this->addChild(littleBoxd);

Then it says that the sprite’s already added>?

How can i kinda clone the sprite from the “Abilities” vector or something so it will work>?

Thanks!

Just store the frame names in the vector instead of sprites. Do the create with frame name inside the loop. Cloning sprites would do effectively the same thing. There are ways to effectively clone sprites so do a search if needed.

I’m on my iPad atm. but do you think it would work out if i stored the frame names as std strings?

Thanks!

yeah. also if you hav a similar format and a lot of quiz sprites you may want to use a loop with a format string using string concatenation (+) or stringstream.

using std::string;
std::vector<string> fnames;
fnames.push_back("ChampSkills/Aatrox_Q.png");
fnames.push_back("ChampSkills/Braum_R.png");

string fname = fnames[0];
Sprite::createWithSpriteFrameName( fname );
1 Like

Yeah thanks again but if i use std::vector instead of cocos2d::Vector do i then have to retain and release then? :frowning:

Thanks!

And i could use the function “getRandomObject” on the vector from cocos2d but how shall i get a random object easily from an std vector?

Thanks for your time!

You do not retain/release std types like string.

int randomIndex = cocos2d::random(0, fnames.size() - 1);
fnames[randomIndex];
1 Like