Search code examples
c++randombox2dcocos2d-xarc4random

Body node shapes with dynamic position


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?


Solution

  • 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.