简体   繁体   中英

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?

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)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM