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

2012/5/29

why do my greeps do this

wahaj wahaj

2012/5/29

#
my greep program has been coming along very nicely. except for one problem. I'll have several greeps on top of a tomato pile (they actually make their way to near the center of the pile rather then just stopping at the graphical extent of the tomato pile image) but they wont pick up the tomatoes. here is the pseudo code if greeps see the tomatoes- find angle between them- turn that way- if the difference between X and Y values of greeps and tomatoes is less than 1 respectively- move.
danpost danpost

2012/5/29

#
That seems about right; that is, the pseudo-code you gave seems to match the behaviour that you are seeing. No where do I see you telling them to pick up a tomato or return to the ship! Think about what the conditions should be for a greep to do this and code it in.
wahaj wahaj

2012/5/29

#
no, i left the pseudo code for them picking up the tomatoes because thats already built into the scenario we are given. here is the code in the Creature class which is a subclass of Actor
/**
     * Load a tomato onto *another* creature. This works only if there is another creature
     * and a tomato pile present, otherwise this method does nothing.
     */
    public final void loadTomato()
    {
        // check whether there's a tomato pile here
        TomatoPile tomatoes = (TomatoPile) getOneIntersectingObject(TomatoPile.class);
        // check whether there's another creature here
        Creature greep = (Creature) getOneIntersectingObject(Creature.class);

        if(greep != null && tomatoes != null) {
            if(!greep.carryingTomato()) {
                tomatoes.takeOne();
                greep.carryTomato();
            }
        }
    }
here is the code which is given in the Greep class which is a subclass of Creature
/**
     * Is there any food here where we are? If so, try to load some!
     */
    public void checkFood()
    {

        // check whether there's a tomato pile here
        TomatoPile tomatoes = (TomatoPile) getOneIntersectingObject(TomatoPile.class);
        if(tomatoes != null) {
            loadTomato();

            // Note: this attempts to load a tomato onto *another* Greep. It won't
            // do anything if we are alone here.
        }
    }
as far as I know these two methods are responsible for loading the food onto another greep. the methods carryTomato() and carryingTomato() are also given in the class Creature and are as follows
/**
     * Check whether we are carrying a tomato.
     */
    public final boolean carryingTomato()
    {
        return carryingTomato;
    }

/**
     * Receive a tomato and carry it.
     */
    private void carryTomato()
    {
        carryingTomato = true;
        setImage(getCurrentImage());
    }
the code I gave is what I'm using to make them stop more or less at the center of the pile in my first post I messed up at the last part of the pseudo- code. its supposed to be f the difference between X and Y values of greeps and tomatoes is greater than 1 respectively- move.
You need to login to post a reply.