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

2014/10/24

Need some help with getNeighbours

Skuggan Skuggan

2014/10/24

#
I'm trying to make a method where all objects of the class DarkWood should be replaced with objects of the class Stone.
         if (isTouching(Star.class))
        {
            List<DarkWood>wood= getNeighbours(getWorld().getWidth(), true,DarkWood.class);
                    
            for (DarkWood darkwood : wood)
            {
                int x = ((DarkWood) getWorld().getObjects(DarkWood.class).get(0)).getX();
                int y = ((DarkWood) getWorld().getObjects(DarkWood.class).get(0)).getY(); 
                getWorld().addObject(new Stone(), x, y);            
            }
            getWorld().removeObjects(getWorld().getObjects(DarkWood.class));
        }
All objects gets replaced but are stacked on top of each other. Some help please!
Super_Hippo Super_Hippo

2014/10/24

#
In line 7 and 8 you get the location of one of the objects and all stones are placed there (line 9).
if (isTouching(Star.class))
{
    for (DarkWood darkwood : getWorld().getObjects(DarkWood.class) )
    {
        getWorld().addObject(new Stone(), darkwood.getX(), darkwood.getY());
        getWorld().removeObject(darkwood);
        /*you can remove this last line and use line 11 of your code outside the for-
        each like you did*/
    }
}
Or is there a reason why you want to use getNeighbours? This didn't cause the problem, but I think it wasn't needed.
Skuggan Skuggan

2014/10/24

#
No, no reason at all I'm afraid. I'm just so new at this that it seemed like a good way to do it. Thank you for your help!
You need to login to post a reply.