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

2014/1/20

Im stuck

swendleys swendleys

2014/1/20

#
I'm having problems with randomly appearing of objects. They touch each other but I don't want them to touch and
public void randomCrates()
    {
     

 addObject(new Krat(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
 addObject(new Krat(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
 addObject(new Krat(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
 addObject(new Krat(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); 
 addObject(new Krat(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }

thanks in advance
danpost danpost

2014/1/20

#
public void randomCrates()
{
    for (int n=0; n<5; n++)
    {
        Krat krat = new Krat(); // create a crate
        addObject(krat, 0, 0);  // add it into the world
        int gap = 5; // space between crates
        int across = krat.getImage().getWidth()+gap; // horiz interval
        int down = krat.getImage().getHeight()+gap; // vert interval
        while (krat.getX() == 0 || krat.getY() == 0 || getObjectsAt(krat.getX(), krat.getY(), Krat.class).size() > 1)
        {
            int x = Greenfoot.getRandomNumber((getWidth()/across)*across)+across/2;
            int y = Greenfoot.getRandomNumber((getHeight()/down)*down)+down/2;
            krat.setLocation(x, y);
        }
    }
}
swendleys swendleys

2014/1/21

#
thank you for the fast reply. But the crates still touch each other. i even increased the int gap but that doesn't worked. I took a screenshot maybe it can help:
bernhard bernhard

2014/1/21

#
How about writing a code in the act-method of the crates that detects wether it is intersecting another one and if that's true it creates a new crates and removes itself.
danpost danpost

2014/1/21

#
Guess I got parenthesis happy! Change lines 12 and 13 to this:
int x = Greenfoot.getRandomNumber(getWidth()/across)*across+across/2;
int y = Greenfoot.getRandomNumber(getHeight()/down)*down+down/2;
swendleys swendleys

2014/1/21

#
here is a follow up question. I want another actor to appear randomly but i don't want is to overlap with the crates. It's already appearing in the world.
        for (int n=0; n<4; n++)  
        {  

            Krat krat = new Krat();
            Krat2 krat2 = new Krat2();
            // create a crate  
            addObject(krat, 0, 0);  // add it into the world  
            int gap = 5; // space between crates  
            int across = krat.getImage().getWidth()+gap; // horiz interval  
            int down = krat.getImage().getHeight()+gap; // vert interval  
            while (krat.getX() == 0 || krat.getY() == 0 || getObjectsAt(krat.getX(), krat.getY(), Krat.class).size() > 1) 
            {  
                int x = Greenfoot.getRandomNumber(getWidth()/across)*across+across/2;  
                int y = Greenfoot.getRandomNumber(getHeight()/down)*down+down/2;  
                krat.setLocation(x, y);  
            }  
            if(n == 3)
            {
                addObject(krat2,0,0);
                krat2.setLocation(1, 1);
            }

        }  
Im sorry to ask so many questions and thank you for your help!!
danpost danpost

2014/1/21

#
You can remove the 'if' block from line 17 to 21 from the 'for' loop and place the inner code of that 'if' block below the 'for' loop. Replace all starting line 15 with:
        krat.setLocation(x, y);
     }
}
addObject(krat2, 0, 0);
In the Krat2 class add the following method:
public void addedToWorld(World w)
{
    while (getX() < 20 || getX() > w.getWidth()-20 || getY() < 20 || getY() > w.getHeight()-20 || !getIntersectingObjects(Krat.class).isEmpty())
    {
        int x = Greenfoot.getRandomNumber(w.getWidth());
        int y = Greenfoot.getRandomNumber(w.getHeight());
        setLocation(x, y);
    }
}
This should add them at a location not too close to an edge and not intersecting any Krat objects. You do not have to code a call to this method; it is automatically called once each time the object is inserted into a world. You could have done this with the Krat class also (but the crates would not be aligned so neatly).
swendleys swendleys

2014/1/22

#
Thank you very much for your help! It works perfectly!!
You need to login to post a reply.