This site requires JavaScript, please enable it in your browser!
Greenfoot back
kiarocks
kiarocks wrote ...

2011/11/27

Adding Objects randomly

1
2
kiarocks kiarocks

2011/11/27

#
I want to add 25 objects at random locations. They need to be in the range of Greenfoot.getRandomNumber(5) + 2.
Morran Morran

2011/11/27

#
Try:
for(int i=0; i < 25; i++) {
   int randX = Greenfoot.getRandomNumber(5) + 2;
   getWorld().addObject(whateverObjectYouNeedToAdd, randX, whateverY);
}
"whateverY" is the Y you need the objects to be at. The for loop should go ahead and create 25 objects, each with a random X, but the same Y. If you want them to have different Y, just do with "whateverY" like what was done with "randX".
kiarocks kiarocks

2011/11/27

#
the problem is getting them not to land on the same spot. Should have clarified that.
danpost danpost

2011/11/27

#
try:
int[][] objsGrid = { {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5} };
for (int loopCt = 0; loopCt < 5; loopCt++)
{
    for (int myCt = 0; myCt < 5; myCt++)
    {
        int x = Greenfoot.getRandomNumber(5);
        int y = Greenfoot.getRandomNumber(5);
        while (objsGrid[x][y] != 5)
        {
            x = Greenfoot.getRandomNumber(5);
            y = Greenfoot.getRandomNumber(5);
        }
        objsGrid[x][y] -= myCt + 1;
    }
}
for (int y = 0; y < 5; y++)
{
    for (int x = 0; x < 5; x++)
    {
        switch(objsGrid[x][y])
        {
            case 0:  addObject(new obj1(), x + 2, y + 2); break;
            case 1:  addObject(new obj2(), x + 2, y + 2); break;
etc. This should put five of each of five different objects randomly within your grid (with none at the same location).
kiarocks kiarocks

2011/11/27

#
there is an error in your code. Keeps adding objects.
danpost danpost

2011/11/27

#
This code would go in your world constructor and should not repeat. You could also put it in a method if you wanted to be called when certain conditions arise (of course, you would have to remove old objects before adding new ones).
kiarocks kiarocks

2011/11/28

#
Oh, i need it in Actor class. It still only runs once.
Morran Morran

2011/11/28

#
You need it to only run once? Maybe you could put it into the addedToWorld(World world) method of that Actor(all Actors's addedToWorld(World world) methods are called when that Actor gets added to the world) Or, if you want it to be added in later on, you could do something like this:
public void act()
{
     timeUntilIShouldAddRandomObjects--;
     if(timeUntilIShouldAddRandomObjects == 0) {
         addRandomObjects();
     }

     //whatever your original act() method had
}
Where timeUntilIShouldAddRandomObjects would be and integer that is set to the number of frames you want until the objects should be added, and addRandomObjects() is danpost's method of adding objects.
kiarocks kiarocks

2011/11/28

#
Nvm, It fell outside the if(dothisonce) statement.
kiarocks kiarocks

2011/11/28

#
How do you suggest i add different amounts of objects?
Morran Morran

2011/11/28

#
Maybe you could do a nested for loop, like:
for(int i=0; i<numOfRows; i++) {
   for(int j=0; j<numOfCols; j++) {
      ObjectToAdd objToAdd = new ObjectToAdd();
      getWorld().addObject(objToAdd, startX + i*spaceBetweenObjects, startY + j*spaceBetweenObjects);
      int rand = Greenfoot.getRandomNumber(amountOfVariation);
      objToAdd.setLocation(objToAdd.getX() + rand, objToAdd.getY() + rand);
   }
}
If you wanted 40 objects(for example), you could set numOfRows to 4 and numOfCols to 10, or 2 and 20, or 5 and 8. This method will only work for a square grid of objects, though.
danpost danpost

2011/11/28

#
int[] maxObjs = { 4, 5, 6, 3, 7};
int[][] objNums = {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5} };
for (int myObj = 0; myObj < 5; myObj++)
{
    for (int myCt = 0; myCt < maxObjs[myObj]; myCt++)
    {
        int x = Greenfoot.getRandomNumber(5);
        int y = Greenfoot.getRandomNumber(5);
        while (objNum[x][y] != -1)
        {
            x = Greenfoot.getRandomNumber(5);
            y = Greenfoot.getRandomNumber(5);
        }
        objsGrid[x][y] = myCt;
    }
}
This would allow different number of objects. Not much of a change!
kiarocks kiarocks

2011/11/29

#
I have a different idea. Thanks though
kiarocks kiarocks

2011/11/29

#
I thought i did, then it got complicated. I need to add things in these denominations. (This was in a for loop.)
if(l < 2)
        {
            cw.addObject(new Light(),ints[l]*64,ints2[l]*64);
            ints[l] = 0;
            ints2[l] = 0;
        }
        else if(l < 8)
        {
            cw.addObject(new Wire(),ints[l]*64,ints2[l]*64);
            ints[l] = 0;
            ints2[l] = 0;
        }
        else if(l < 12)
        {
            cw.addObject(new Resistor(),ints[l]*64,ints2[l]*64);
            ints[l] = 0;
            ints2[l] = 0;
        }
        else if(l < 15)
        {
            cw.addObject(new Diode(),ints[l]*64,ints2[l]*64);
            ints[l] = 0;
            ints2[l] = 0;
        } 
        else if(l <= 25)
        {
            cw.addObject(new EmptyCircuitArea(),ints[l]*64,ints2[l]*64);
            ints[l] = 0;
            ints2[l] = 0;
        }
kiarocks kiarocks

2011/11/29

#
still 25 objects.
There are more replies on the next page.
1
2