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

2011/10/30

Help with getting animal to goHome after something is done

keiko1dog keiko1dog

2011/10/30

#
Hi! Very new to Greenfoot and Java. I need to get one of my Actors(seal) to return to its Habitat after it has eaten a fish. I have written all the code for the seal to move around the world, eating fish as it comes across one but I need the seal to return to its Habitat or home after each time it eats a fish, then wait a few seconds, then return to the world to eat some more. Like I said, VERY new to this so any help would be greatly appreciated!!!
danpost danpost

2011/10/30

#
Tell yourself exactly what you want the actor to do, and half the code is already there. Say: If see food, eat food. if ate foot, migrate home. if home, wait2seconds. if none of the above, move around. What do we have?
if (restTime > 0)
{
    restTime--;
    if (restTime == 0) ateFood = false;
    return;
 } 
if (ateFood) { migrateHome(); return; } // migrateHome checks ?isHome()? and sets restTime to 20 (whatever suits)
if (seeFood()) { eatFood(); return; } else { moveAround(); } // eatFood sets ateFood to true
// You already have this coded
I used two instance variable for the seal class: 'restTime' (of type integer) and 'ateFood' (of type boolean). There are four (4) methods, migrateHome would be the one in question. What action, exactly, do you want from the seal at this point? (Tell yourself).
You need to login to post a reply.