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

2011/11/17

Help With Greeps

carterfootball carterfootball

2011/11/17

#
I dont know what happened but my greeps started to stop when they get back to the ship can anyone help me? Heres my code for Greep.class import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.util.List; /** * A Greep is an alien creature that likes to collect tomatoes. * * @author (your name here) * @version 0.1 */ public class Greep extends Creature { // Remember: you cannot extend the Greep's memory. So: // no additional fields (other than final fields) allowed in this class! /** * Default constructor for testing purposes. */ public Greep() { this(null); } /** * Create a Greep with its home space ship. */ public Greep(Ship ship) { super(ship); } /** * Do what a greep's gotta do. */ public void act() { super.act(); // do not delete! leave as first statement in act(). if (carryingTomato()) { if(atShip()) { dropTomato(); move(); } else { if(randomChance(10)) { turnHome(); atLake(); } atLake(); checkFood(); atLake(); move(); } } else { atLake(); checkFood(); atLake(); xyz(); } } /** * turns 90 deg. if at water. */ private void atLake() { if(atWater()) { turn(Greenfoot.getRandomNumber(30)); xyz(); } else { xyz(); } if(atWorldEdge()) { turn(Greenfoot.getRandomNumber(8)); xyz(); } } public void xyz() { Greep greeps = (Greep) getOneIntersectingObject(Greep.class); if(!getFlag(1)) { move(); } if(getFlag(1)) { stop(); } } /** * 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); Greep greeps = (Greep) getOneIntersectingObject(Greep.class); if(tomatoes != null) { setFlag(1, true); loadTomato(); // Note: this attempts to load a tomato onto *another* Greep. It won't // do anything if we are alone here. } } /** * This method specifies the name of the author (for display on the result board). */ public static String getAuthorName() { return "Tank"; // write your name here! } /** * This method specifies the image we want displayed at any time. (No need * to change this for the competition.) */ public String getCurrentImage() { if(carryingTomato()) return "greep-with-food.png"; else return "greep.png"; } public void stop() { move(0); } }
davmac davmac

2011/11/18

#
When you pick up food, you call setFlag(1, true). In the xyz() method, you check the flag and do not move if it is set. However, you never clear the flag, so once a greep has dropped its tomato at the ship it will stop moving. So, you need to clear the flag when you have dropped the tomato at the ship.
mik mik

2011/11/18

#
Let me just say again, though: Please don't post full source code here for the Greeps competition. It's a competition that is run regularly in many places, and the whole point is that you do this yourself. If you post all your code here, you are making it too easy for others to just copy your code. So, it's okay to discuss ideas, strategies an options, and to get general help on solving your problems, but please no posts of full source code for this one. Thanks.
You need to login to post a reply.