Problem with a loop

i use an iterator for the food2 because i had an error when i use the same template

well i dont initialize the position because i dont know how (i was thinking thats what i do when i add them with grow.
I was thinking the return position was fine, its work when my creatures eat it on the ground randomly.

what do you mean by ArrayList<PVector> ? and yes i surely mix some thing :slight_smile:

Its not that it is wrong, it’s just that you don’t really need it. You need “getter” and “setter” function when your variables are private; and they should be to be compliant with the idea of encapsulation. In your case, the variable is public so you can access it directly without using a function.

You are not setting the position variable, you are adding a new PVector to your food2 arrayList. So your position variable stays undetermined.

I meant ArrayList<PVector> food2;. Something went wrong with the copy/pasting. I ask that because you have a class Food2 that should represent your food and that have a position. But there is also that list that seems to also be food but that is just PVector object and not Food2 object.

for the ArrayList<PVector> food2; i have that with all my class, if i dont put that line i got an java null pointer exception.

for the position i think i get what you mean but i dont know how to fix it

i think its because i mix java and the Processing library

ok correction i have it also in my creature class but it does nothing i just tried

It seems odd but as long as it works for you. I would maybe try to rethink the design a bit though.
It seems that what you want is an array of Food2 in your word and the grow function should add Food to that array and not inside the Food2 object.

Anayway, to fix your problem Try the following:

PVector target = new PVector(0,0);
currentSmallest = (parent.height * parent.height) + (parent.width * parent.width);
				
for(Iterator<Food2> i = food2.iterator(); i.hasNext();){
  Food2 f = i.next();
  ArrayList<PVector> foodList = f.getFood2()

  for (int i = 0; i < foodList.size(); i++) {
    PVector food = foodList.get(i);
    dist = ((h3f.position.x - food.x) * (h3f.position.x - food.x)) + ((h3f.position.y - food.y) * (h3f.position.y - food.y));

    if(dist < currentSmallest){
      currentSmallest = dist;
      target.x = f.position.x;
      target.y = f.position.y;
    }
  }
}

yes but im too scared too ask help for that :slight_smile: .

now the java null pointer exception are on

target.x = f.position.x;
target.y = f.position.y;

Yep sorry I forgot to edit that too:

target.x = food.x;
target.y = food.y;

maybe i did something wrong here is the code

float currentSmallest;
float dist;
for(int a = herb3f.size()-1; a >= 0; a--){
	Herbivor3F h3f = herb3f.get(a);
	if(h3f.faim() == false){
		h3f.randomMv();
	}
	else{
		PVector target = new PVector(0,0);
		currentSmallest = (parent.height * parent.height) + (parent.width * parent.width);
					
		for(Iterator<Food2> i = food2.iterator(); i.hasNext();){
			Food2 f = i.next();
			ArrayList<PVector> foodList = f.getFood2();
				
			for(int b = 0; b < foodList.size(); b++){
				PVector food = foodList.get(b);
				dist = ((h3f.position.x - food.x) * (h3f.position.x - food.x)) + ((h3f.position.y - food.y) * (h3f.position.y - food.y));
				if(dist < currentSmallest){
					currentSmallest = dist;
					target.x = f.position.x;
					target.y = f.position.y;
				}
			}
		}
		h3f.findFood(target);
	}
}

yeah i should see that :slight_smile:

its work !!!

wow they run to the food like fly on honey :smiley:

i dont know how to thank you enough !

1 Like

Hapy to hear that :slight_smile:

Now try to really understand what I did and why your solution was not working.

2 Likes

yes thats my homework for today i guess :slight_smile: