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

2015/10/13

How to examine the accordence of the the localisation of two objects

RuinaImperii RuinaImperii

2015/10/13

#
I have 5 Objects food and 100 ants and i have to examine the accordence of the location of an ant and a food. The food has a permanent location, the ants are moving randomly. I implimented the ants and the food in the Worldclass and want to examine the location of the food with an ant. Here I implimented the food and ants: public class Waldboden extends World { /** * Constructor for objects of class Waldboden. * */ public Forest() { super(500, 500, 1); int a = new int ; for (int i = 0; i<100; i++){ Ant ant = new Ant(); addObject(ant, 250, 250); addObject(ant, 250, 250); } } for (int k = 0; k<5;k++){ Food food = new Food; double x = Math.random()*500; double y = Math.random()*500; addObject(food, (int)x, (int)y); //addObject(food, (int)x, (int)y); } } } Here I am examining the locations: public class Ameise extends Actor { public void act () { this.turn(); this.move(); int aktuellesX = this.getX(); int aktuellesY = this.getY(); for (int k =0;k<5;k++){ if (this.getX() == food.x && this.getY == food.y){ if (food.x<250){ int xSteps=250/food.x; int merker=250%food.x; }else{ xSteps=food.x/250; merker=food.x%250; } But it won't compile, because Greenfoot reports "cannot find simbol- variable food" what do i have to change?
danpost danpost

2015/10/13

#
The variable 'food' is declared in the constructor of the Forest class, and then, within a for loop. It is not recognized anywhere else within that class or any other class as far as I can tell by what was given. It is only valid after declared within the for loop up to the end of that for loop.
You need to login to post a reply.