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

2013/12/16

I need help with greeps !!!

chrissilive chrissilive

2013/12/16

#
I have inserted set rotation and set location , which actually brings a lot of point. However, it does not work properly. The greeps jump over the water and circle around in the corner. Please help me! :-) --> This is my code. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * 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(); turn(180); } else { bewegen(); if(randomChance(28)) {spit("red"); turnHome();} } } else { checkFood(); } } /** * 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. } else { bewegen(); } } public boolean seeFood() { if (getOneIntersectingObject(TomatoPile.class) == null) { return false; } else { return true; } } public void turnToTomatoes() { if (seeFood()) { int deltaX = getOneIntersectingObject(TomatoPile.class).getX() - getX(); int deltaY = getOneIntersectingObject(TomatoPile.class).getY() - getY(); setRotation((int) (180 * Math.atan2(deltaY, deltaX) / Math.PI)); } } /** * This method specifies the name of the author (for display on the result board). */ public static String getAuthorName() { return "Anonymous"; // 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"; } /** * dat Wasser */ public void bewegen() { move(); if(atWater()) { turn(5); setLocation (getX() + 10, getY() -3); // Moves the object 4 cells to the right setRotation (getRotation() + 180); // Rotates the object 90 degrees clockwise move(); } if(atWorldEdge()) { setLocation (getX() + 55, getY() -55); // Moves the object 4 cells to the right setRotation (getRotation() + 38); // Rotates the object 90 degrees clockwise } } }
danpost danpost

2013/12/16

#
From what I understand, 'setLocation' is not allowed in the greeps scenario (check the rules).
RULES wrote...
Rule 7: No teleporting. Methods from Actor that cheat normal movement (such as setLocation) may not be used.
chrissilive chrissilive

2013/12/16

#
yeah i know but i wanted to know this. just for fun
danpost danpost

2013/12/16

#
Well, this statement:
setLocation (getX() + 55, getY() -55);
does not move the object 4 cells to the right (as documented). The following would:
setLocation(getX()+4, getY());
However, I would think you would do something different depending on which world edge was encountered.
You need to login to post a reply.