SAME ORDER! Please help....

Hi!
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


EVERYtime.jpg (338.4 KB)

@nichlaspro132

What’s the code behind ?

CCString string = (CCString)Utils::getGems()->randomObject();

Thanks for the quick reply!
the for the Utils::getGems() is:
CCArray* Utils::getGems()
{
CCArray* ary = CCArray::create();
for (int i = 1; i <= 6; i++) {
CCString* fileNum = CCString::createWithFormat(“gemstone%d.png”, i);
ary->addObject(fileNum);
}
return ary;
}

PLZPLZ Help ME xD:)

@nichlaspro132

You should call srand(time(NULL)) before calling CCString string = (CCString)Utils::getGems()->randomObject();

TY xD!
But now there comes only red on the screen…
why!:slight_smile:
I Appreciate Your HELP!

gem->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString()));

You are setting the same spriteFrame to each sprite you created, you should try to let the spriteframe name be random.

How xD:)
just please give me a little example please…
I’ve tried this: gem->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache->spriteFrameByName((CCString)Utils::getGems()->randomObject)));
but that does not work!
it just gives the same result!
U R MY GOD IF U CAN HELP!

PLZPLZPLZ HELP!

@nichlaspro132
Do you call srand(time(NULL)) this method before calling the getRandomObject?

Hi xD!
i’ve tried that, this is the code:

void Game::showMole()
{
srand(time(NULL));
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++;
}
I DID ALL WHAT U SAID:(
Can u plzplzplz TRY to figure whats wrong, it does still just give the same order, now it is just my purple that comes up…
And I REALLY APPRECIATE YOUR HELP AND TIME!:slight_smile:

You should only initialize srand one time, at the beginning of your app, not each time you are going to use rand.

srand set the seed used to initialize the random generator.

Please use the inline code formatter too :wink:

LOVE U BOTH!!!
IT WORKED!!!(/) :slight_smile: :slight_smile: :slight_smile: :slight_smile:
MY LIFE IS SAVED!