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

2013/1/12

how to make one greep stop at a tomato a i need only one???

hossam hossam

2013/1/12

#
in the greep game my problem is more than one greeps stop at tomato although i put in my code else if (greeps==null) { move(); }
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
If I understood the problem right you probably are checking whether there is a tomato at the position of the greep. Pobably with some code like this:
if (!getWorld().getObjectsAt(getX(), getY(), Tomato.class).isEmpty()) {
    //stop moving because of the tomato;
}
If you now want only one greep to stop at the tomato it should work like this:
if (!getWorld().getObjectsAt(getX(), getY(), Tomato.class).isEmpty() && !otherGreepAtThisPosition()) {
    //stop moving because of the tomato and no other greep;
}

//to use this method you have to import java.util.List;
public booleanOtherGreepAtThisPosition() {
    List<Greep> greeps = getWorld().getObjectsAt(getX(), getY(), Greep.class);
    if (!greeps.isEmpty()) {
        for (Greep greep : greeps) {
            if (!this.equals(greep)) {
                return true;
            }
        }
    }
    return false;
}
hossam hossam

2013/1/12

#
listen what i wrote is public void stopAtTomato() { TomatoPile tomatoes = (TomatoPile) getOneIntersectingObject(TomatoPile.class); Greep greeps = (Greep) getOneIntersectingObject(Greep.class); if(!getFlag(2)) { move(); } if(getFlag(2) ) { if (tomatoes != null && greeps ==null ){ move(0); } else { if (greeps!=null && tomatoes == null){ move(); } } } }
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
What exactly is the problem in this code and what does the method getFlag(int) do?
hossam hossam

2013/1/12

#
getFlag(int); and setFlag(int, bolean true or false) so it only takes 1 or 2 so the greep has a small memory that it help them to store one place by setFlag(1,true); in the place you want so in my case i setFlag(2); when the greep the tomato to stop the problem with this code is this code make more than one greep stop at tomato i need only one greep to stop at tomato.
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
did you try the code I posted before?
hossam hossam

2013/1/12

#
no because i didnt understand what this method do???
hossam hossam

2013/1/12

#
your methods is not working it stops every thing it compile but there is something wrong in it (logic error) ???!!!!
You need to login to post a reply.