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

2017/3/29

getNeighbors Help

1
2
rockon411 rockon411

2017/3/29

#
I would like to use getNeighbors() and remove the neighbors around my actor. I know getNeighbors returns a list, so how do I use the remove() method which only removes an actor?
danpost danpost

2017/3/29

#
rockon411 wrote...
I would like to use getNeighbors() and remove the neighbors around my actor. I know getNeighbors returns a list, so how do I use the remove() method which only removes an actor?
First create the list using 'getNeighbors'. Then make sure the list is not empty. If not empty, use the size to randomly pick one of the objects from the list to remove from the world using 'removeObject'.
rockon411 rockon411

2017/3/30

#
So I created the list and make sure that it's not empty. I'm kind of confused as to what to do next.
1
2
3
4
5
6
neighbors = getNeighbors(1, true, Bee.class);
        while (neighbors.size() > 1)
        {
            getWorld().removeObjects(neighbors);
        }
        getWorld().remove(getOneIntersectingObject(Bee.class));
Currently I'm receiving an error "cannot find symbol - method removeObjects(java.util.List)."
danpost danpost

2017/3/30

#
You do not need a field for the list -- a local variable will suffice. Also, I will presume that the code given is in your act method (or in a method called by your act method). Line 2 is no correct in two ways: it should be 'if', not 'while'; and it should check for a size greater than zero, not one. Finally, I see that you are only looking one cell away. I would presume then that your world has a cell size that is much greater than one; but, then you are using 'getOneIntersectingObject' on the last line, which seems to be a contradiction. Currently, I can only give the following as a replacement for the code above to remove just one of its neighbors:
1
2
3
4
5
List neighbors = getNeighbors(1, true, Bee.class);
if (!neighbors.isEmpty())
{
    getWorld().remove((Actor)neighbors.get(Greenfoot.getRandomNumber(neighbors.size())));
}
rockon411 rockon411

2017/3/30

#
I'm currently trying to remove all of the neighbors within a one cell radius and any intersecting objects. Thank you for your help!
Nosson1459 Nosson1459

2017/3/30

#
danpost wrote...
You do not need a field for the list -- a local variable will suffice. Also, I will presume that the code given is in your act method (or in a method called by your act method). Line 2 is no correct in two ways: it should be 'if', not 'while'; and it should check for a size greater than zero, not one. Finally, I see that you are only looking one cell away. I would presume then that your world has a cell size that is much greater than one; but, then you are using 'getOneIntersectingObject' on the last line, which seems to be a contradiction. Currently, I can only give the following as a replacement for the code above to remove just one of its neighbors: <Code Omitted>
For your code it will have to be:
1
2
3
4
List neighbors = getNeighbours(1, true, Bee.class);
if (!neighbors.isEmpty()) {
    getWorld().removeObject((Actor) neighbors.get(Greenfoot.getRandomNumber(neighbors.size())));
}
rockon411 wrote...
I would like to... remove the neighbors around my actor. I know getNeighbors returns a list, so how do I use the remove() method which only removes an actor?
If you want to remove all the surrounding bees then you can simply do:
1
getWorld().removeObjects(getNeighbours(1, true, Bee.class));
rockon411 rockon411

2017/3/30

#
When I type that it won't compile and it says "cannot find symbol - method removeObjects(java.util.List<Bee>)"
Nosson1459 Nosson1459

2017/3/30

#
rockon411 wrote...
I'm currently trying to remove all of the neighbors within a one cell radius and any intersecting objects. Thank you for your help!
If two actors are on the same cell than they are intersecting (maybe you mean that the getNeighbours should be a radius of two, but either way there is still no point in checking for neighbors and intersecting).
rockon411 wrote...
When I type that it won't compile and it says "cannot find symbol - method removeObjects(java.util.List<Bee>)"
When I use just my provided line it compiled and works.
rockon411 rockon411

2017/3/30

#
I have this written and when I look at the documentation for the world. I only see a remove method not a removeObjects method. I am going into office hours tomorrow, so if you can't figure it out I'll survive :).
1
2
3
4
5
public void explode()
    {
            getWorld().removeObjects(getNeighbors(1, true, Bee.class));
 
    }
danpost danpost

2017/3/30

#
How about this:
1
for (Object obj : getNeighbors(1, true, Bee.class)) getWorld().remove((Actor)obj);
Nosson1459 Nosson1459

2017/3/30

#
rockon411 wrote...
I have this written and when I look at the documentation for the world. I only see a remove method not a removeObjects method. I am going into office hours tomorrow, so if you can't figure it out I'll survive :). <Code Omitted>
Are we talking about the same thing, because in my greenfoot.World documentation I have two remove methods:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * Remove an object from the world.
 *
 * @param object the object to remove
 */
public void removeObject(Actor object)
{
    if (object == null || object.world != this) {
        return;
    }
     
    objectsDisordered.remove(object);
    collisionChecker.removeObject(object);
    if (objectsDisordered != objectsInActOrder && objectsInActOrder != null) {
        objectsInActOrder.remove(object);
    }
    else if (objectsDisordered != objectsInPaintOrder && objectsInPaintOrder != null) {
        objectsInPaintOrder.remove(object);
    }
    object.setWorld(null);
}
 
/**
 * Remove a list of objects from the world.
 *
 * @param objects A list of Actors to remove.
 */
@SuppressWarnings("unchecked")
public void removeObjects(Collection<? extends Actor> objects)
{
    for (Iterator<? extends Actor> iter = objects.iterator(); iter.hasNext();) {
        Actor actor = iter.next();
        removeObject(actor);
    }
}
I don't see any plain remove method.
danpost wrote...
How about this:
1
for (Object obj : getNeighbors(1, true, Bee.class)) getWorld().remove((Actor)obj);
I was going to suggest this but I then saw the removeObjects method. Do you have it? See here. I'm using Greenfoot Version 3.1.0 but I think these methods have not been changed.
danpost danpost

2017/3/30

#
Nosson1459 wrote...
< Quote w/Code Omitted > I was going to suggest this but I then saw the removeObjects method. Do you have it?
No. I could only guess that rockon411 is using a version of greenfoot that has been modified and re-packaged.
I'm using Greenfoot Version 3.1.0 but I think these methods have not been changed.
I am still using Greenfoot Version 2.3.0 USB standalone which I have no problems with.
rockon411 rockon411

2017/3/30

#
So that line worked and I really really really appreciate it guys! Thank you so much!
Nosson1459 Nosson1459

2017/3/30

#
danpost wrote...
I am still using Greenfoot Version 2.3.0 USB standalone which I have no problems with.
I might agree that there are no problems with it but I never tried it. What's wrong with the later versions, if you updated it will make things easier (in discussions and scenarios). If you look here then you'll see a lot of things that have been fixed since then.
danpost danpost

2017/3/30

#
Nosson1459 wrote...
What's wrong with the later versions, if you updated it will make things easier (in discussions and scenarios).
It would probably make it much easier in discussions since most users are using later versions. However, the changes that have been made in later versions than what I have are relatively minor, until this latest change in the Color and Font classes. About the only thing I had to deal with prior to that was adding 'isAtEdge' methods to classes where the method is used after downloading other people's scenario. I am sorry (to greenfoot) to say that I have not felt very confident about the later versions. Also, I am under the impression that they have gone astray from their primary goal by many of the changes (including the addition of the 'isAtEdge' method). I would stick with the basic adage -- K.I.S.S., "keep it simple....".
There are more replies on the next page.
1
2