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

2012/11/25

Mutliple random placements

1
2
Wavesludge Wavesludge

2012/11/25

#
Hi. I want to place 4 instances of my Obstacle class in the world at random locations. When I've tried doing it I only see one object, but I want them at different locations. How would I go about doing this?
Gevater_Tod4711 Gevater_Tod4711

2012/11/25

#
You probably have done this:
int x = Greenfoot.getRandomNumber(1000);//the width of your world;
int y = Greenfoot.getRandomNumber(1000);//the heigth of your world;

addObject(new Obstacle(), x, y);
addObject(new Obstacle(), x, y);
//...
but then x and y are the same values in all cases. So you better do it like this:
for (int i = 0; i < 4; i++) {
    addObject(new Obstacle(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
}
Wavesludge Wavesludge

2012/11/25

#
Thank you very much!
Wavesludge Wavesludge

2012/11/25

#
One more thing. I have 4 images that I want the obstacles to randomly get, so every one isn't the same. I have used this:
setImage("Obstacle" + Greenfoot.getRandomNumber(4) + ".png"); 
But it acts like a loop, so every obstacles goes through every 4 images, it looks like a gif. How would I do to make it so it's not like a loop, the obstacles just get one of the four images?
danpost danpost

2012/11/25

#
Sounds like you put that statement in the act method, instead of the constructor of the class.
Gevater_Tod4711 Gevater_Tod4711

2012/11/25

#
it looks like a loop because the image changes every act because you have put this code in the act method. If you just get it out if the act method and put it into the constructor it'll work.
Wavesludge Wavesludge

2012/11/25

#
If I put it in the constructor I get an error: Invalid method declaration; return type required
Gevater_Tod4711 Gevater_Tod4711

2012/11/25

#
that just means your declaration of the constructor is wrong. A constructor has to look like this:
public class AnyClass {
   public AnyClass() {
        //your code you want to put in the constructor;
   }
}
danpost danpost

2012/11/25

#
Need to see your constructor.
Wavesludge Wavesludge

2012/11/25

#
Your soloution worked Gevater, thanks! I forgot to do the public AnyClass()
Wavesludge Wavesludge

2012/11/27

#
One problem I've been having. I'm having two classes placing randomly, and sometimes they land ontop of eachother.
    private void ritem()  
    {  
        if (getObjects(Obstacle.class).isEmpty())  
        {  
            for (int i = 0; i < 7; i++) {  
                addObject(new Obstacle(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));  
            } 
        }  
        
        if (getObjects(Food.class).isEmpty())  
        {  
            for (int i = 0; i < 1; i++) {  
                addObject(new Food(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));  
            } 
        }
    }
How could you make so that doesn't happen?
alainmoran alainmoran

2012/11/27

#
Change your addObject so that it generates the positions within itself and validates that nothing else exists at that location (if something does then it should generate a new position pair)
Wavesludge Wavesludge

2012/11/27

#
OK, I don't know how to do that. Still new to greenfoot. Could you give an example?
Gevater_Tod4711 Gevater_Tod4711

2012/11/27

#
I think an easyer way would be to change the locations of the objects if they intersect each other. You could do it like this:
//in the class you want to add some objects of;

private boolean initialised = false;//you need this because you cant write it in your constructor;

public void act() {
    if (!initialised) {
        while (!getIntersectingObjects(ClassA.class).isEmpty() || !getIntersectingObjects(CalssB.class).isEmpty()) {// you need to check for every class you don't want this object to intersect with;
            setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
        }
        initialised = true;
    }
}
Wavesludge Wavesludge

2012/11/27

#
Worked like a charm, thanks!
There are more replies on the next page.
1
2