سؤال

I want to display the shapes with dynamic position.

Ex..

     A      B      C     D
     D      A      B     E
     E      B      D     A

Here the coding:

string names[] = {
"AB",
"ABC",
"ABCZ",
"ABCDEDF",

};

void HelloWorld::addShapes(HelloWorld* game)
{
name = names[arc4random()%4];
......

.....

CCPoint pos1[8];
for (int i = 0; i< TempNumOne; i++)
{
    pos1[i]=CCPoint(disx, disy);
}


for (int a=0; a<TempNumOne; a++)
{
    Filename[a]=FileMeasure[a];
    int temp= arc4random()%TempNumOne;
    ......
    bodyDef.position.Set(pos1[temp].x/32.0f, pos1[temp].y/32.0f);
    .....

    switch (Filename[a])
    {
        case 'A':
        {
           ......
        }
        case 'B':
        {
            ......
        }
        etc.....
     }

All the logic working fine except dynamic position.

Sometime arc4random function returns the same values in the looping statement. I have same position for two shapes.

I want to display the shapes different position.

Can any one assist me?

هل كانت مفيدة؟

المحلول

You can't use random that way. It may return the same values (that's how random works). What you need is random_shuffle

std::string[] names = {"A", "B", "C"};
std::random_shuffle(std::begin(names), std::end(names));
//now names are in random order. just iterate over them.

نصائح أخرى

If you want unique random numbers you need to implement it... You can remember all previous numbers and check if next number is unique, but its non-deterministic algorithm. Or use better algorithm like Knuth-Fisher-Yates algorithm: Unique (non-repeating) random numbers in O(1)?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top