RANDOM OBJECT!

Hi Again!
i would add random object from a ccArray to the scene and the array holds 6 Gems(sprite children) but the second time i run the function it says: this child has already been added. So my overall question is: HOW CAN I GET INFINITY NUMBER OF RANDOM CHILDREN FROM THAT ARRAY TO MY SCENE!
here’s all the info:
-Code:

void Game::onEnterTransitionDidFinish()
{
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("GEMS.plist");
    this->setTouchEnabled(true);
    this->initializeGame();
}

void Game::initializeGame()
{
    gems = CCArray::create();
    gems->retain();
    gemsOnScreen = CCArray::create();
    gemsOnScreen->retain();
    
    for (int i = 1; i <= 6; i++) {
        CCString *filename = CCString::createWithFormat("gemstone%d.png", i);
        Gem* gem = (Gem*)Gem::createWithSpriteFrameName(filename->getCString());
        gems->addObject(gem);
    }
}

void Game::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
    CCTouch* touch = (CCTouch*)pTouches->anyObject();
    CCPoint location = touch->getLocationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);
    
    showMole();
}
void Game::showMole()
{
    Gem* gem = (Gem*)gems->randomObject();
    
    
    gem->setPosition(ccp(150*timesX, 400));
    this->addChild(gem);
    gemsOnScreen->addObject(gem);
    timesX++;
}

void Game::onExit()
{
    gems->release();
    gemsOnScreen->release();
    CCLayer::onExit();
}

BUT SECOND TIME I TOUCH AND IT RUNS SHOWMOLE() IT SAYS “THIS CHILD HAS ALREADY BEEN ADDED”.
PLZPZLZPLZPLZPZLPZL HELP ME!:slight_smile: :frowning:
IMAGE OF THE ERROR:


MyError.jpg (430.7 KB)

I do this and dont have a problem, but I do it differently.

just as an example, what about something like:

CCSprite *spriteArr[ 100 ];

for ( unsigned int i = 0; i < 100; i++ ) {

    spriteArr[ i ] = new cocos2d::CCSprite();
    spriteArr[ i ]->initWithFile( "sprite.png" );
    spriteArr[ i ]->setPosition( cocos2d::CCPoint( i * 10, 100 ) );

    this->addChild( spriteArr[ i ] );

}

for randomness, maybe use something like:

      std::random_device rseed;
      std::mt19937 rgen(rseed()); // mersenne_twister
      std::uniform_int_distribution<int> idist(1, spriteArr.size()); // [0,100]
      int _random = idist(rgen);

This is just psuedo code, I assumed you were on Cocos2d-x v2

OMG TYYYY~!
But i wanted to add more children from one object in an array expempel:
Gem* gem = (Gem*)array->randomObject;
this->addChild(gem);
And then run that function more than once!
BUT how can i do that without getting “this child has already been added”…
I REALLY APPRECIATE YOUR ANSWER BUT CAN U OR SOMEONE ELSE TELL ME THE ANSWER FOR THIS QUESTION:)

Look at my For loop carefully. It is doing what you want. Adding multiple’s and you wont get the error.

Many thanks again it worked!
But SOMEONE PLZPLZPLzPLZ tell me why this happens:

I schedule this tick function where i should get a random object.
But it is always in the same order, why and how can i actually make it random…
See the image in the bottom to see the order…:slight_smile:

void Game::tick(float dt)
{
timeElapsed += dt;
increaseElapsed += dt;
totalTime += dt;
if (timeElapsed >= TimeBetweenGems ) {
this->showMole();
timeElapsed = 0;
}
}

And the showmole() definition

void Game::showMole()
{
CCString string = (CCString)Utils::getGems()->randomObject();
Gem* gem = Gem::create();
gem->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString()));
gem->setPosition(ccp(100*timesX, 400));
this->addChild(gem);
gemsOnScreen->addObject(gem);
timesX++;
}

Just ask for more info if its needed.
PLZ PLZ PLZ ZPLZ HELP ME AGAIN xD:)

I already helped you! Please try my code and see that it works,

You get your error because you dont have your objects in a container so you really are creating multiple gem objects and adding it. It works the first time but 2nd time it knows you already have added one.

OMG LOVE U!
I understand, but can u please give me an example:)
U r my god!

PLZZZZ xD:)

I already gave you the exact answer, just substitute in your Gem object.