Hello, I'm pretty much a fresh beginner to Greenfoot and recently have come across some problems which I seem not to be able to tackle on my own.
First of all I have a problem with the turn() method. I don't want it to rotate my object in the direction of the move. I've tried the solutions written here http://www.greenfoot.org/topics/2318 but these methods seem to supersede the move method, i.e. the objects are made to go randomly around, and setRotation makes them go in certain direction:/
Secondly I'd like certain objects to dissapear at certain locations. The project I'm working on is a simulation of a restaurant in which waiters are moving and attend the tables. Waiter 0 looks for table 0 - if it reaches a table 0 (the square around it) it should dissapear (and change the table number to 1 in the process) and if it reaches table 1 or 2, it won't dissapear, but will have to avoid steping into that table. It should also be able to avoid another waiter (not step into him)
Here's my code. I figured out the random movement in the world and the colision detection with it's borders, but the turning and dissapearing problem is just impenetrable for me atm.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; // Potrebujeme pracovat se seznamem (java.util.List) /* * Trida, predstavujici cisniky */ public class Waiter extends Actor { //Celkovy plat, reprezentovany jako pocet kol vsech cisniku private static Integer wages = 0; /* * Vynulovani statistik (pri vytvareni nove inhstance sveta) */ public static void resetStatistics() { wages = 0; } /* * Vraci celkovy plat vsech cisniku */ public static Integer getWages() { return wages; } //Typ cisnika private Integer type; // Cisnik je vytvaren primo z restaurace, ktera rozhoduje o jeho typu public Waiter(Integer type) { this.type = type; //Na disku jsou soubory waiter_0.png, waiter_1.png a waiter_2.png setImage("waiter_"+type+".png"); } public void act(){ move (1); if (Greenfoot.getRandomNumber(100)<65) { turn(Greenfoot.getRandomNumber(180)-90); } if (getX() <= 1 || getX() >= getWorld().getWidth() - 1) { turn(45); } if (getY() <= 1 || getY() >= getWorld().getHeight() - 1) { turn (45); } if ((getX() == 1) && (getY() == 1)){ turn (180); } if (Greenfoot.getRandomNumber(1000)==0) { ((BlueWorld)getWorld()).removeWaiter(); getWorld().removeObject(this); } else { //TODO: Doplnit interakci s ostatnimi objekty (podle zadani) } wages++; //penize za kazde kolo aktivniho cisnika moveAndTurn(); //TODO: Doplnit pohyb cisnika } } public void eat(){ Actor waiter; waiter = getOneObjectAtOffset(0,0, Actor.class); if (waiter != null) {World world=getWorld(); world.removeObject(waiter); } } }