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

2013/10/28

adding trees

RedCreeper RedCreeper

2013/10/28

#
How would I make it so that a world 1000x1000 adds a tree everywhere but within 60px of another actor
There's two ways I can see doing this, depending on what you want. You can place them evenly, or randomly. Evenly is easy:
for (int x = 0; x < getWidth(); x+=60)
    for (int y = 0; y < getHeight(); y+=60)
        addObject(new Tree(), x, y);
Randomly is a little bit trickier, you have to choose a random spot, then make sure it's not within 60 pixels of another tree (use the distance formula or something like the distance class found in my: Dynamic Lighting. If it's less than 60, choose a new spot and try again, if it's bigger than 60, place a tree. You have to determine how many trees you want, or programmically find out how much you can place in the area, to see if it's "full" or not.
danpost danpost

2013/10/28

#
I would first add the trees to the world (before adding the actor in question). Then add the actor at the location you want to place it. In the class of that actor, add the following method:
public void addedToWorld(World w)
{
    w.removeObjects(getObjectsInRange(60, Tree.class));
}
You do not have to call this method from anywhere; it will automatically execute when you 'addObject' the actor into the world.
Oh... I misread what he wrote. Yah, danpost's is super efficient and works great for what you want.
angosso angosso

2013/10/28

#
The Plural Diasporas
You need to login to post a reply.