Why do you insist on looping through the food array? The update code I posted works just fine:
Code:Creature creature1;
int numFood = 10;
Food[] food;
int counter;
int currentTarget = -1;
//int ctX,ctY;
boolean[] isAlive = new boolean [numFood]; //state of ellipses, true in the beginning
boolean col;
void setup() {
size (600,600);
creature1 = new Creature (-10,-10,15,15); //starting position x & y, initial size
food = new Food[numFood];
for (int i=0; i < numFood; i++) {
float pelX = 1000;
float pelY = 1000;
food[i] = new Food (10,10, pelX, pelY); //initial size, position of food x & y
isAlive[i] = true;
}
}
void draw () {
background (255);
for (int i=0; i < numFood; i++) {
food[i].display();
}
creature1.update();
creature1.display();
}
class Creature {
float x;
float y;
float cWidth;
float cHeight;
Creature (float xp, float yp, float cw, float ch) {
x = xp;
y = yp;
cWidth = cw;
cHeight = ch;
}
void update () {
// if currentTarget is a kludge - what you really need is a boolean
// to determine whether there are any eatable foods on screen...
if(currentTarget != -1) {
x += ((food[currentTarget].posX)-x)/10;
y += ((food[currentTarget].posY)-y)/10;
if (abs((food[currentTarget].posX)-(x)) <= 15 && abs((food[currentTarget].posY)-(y)) <= 15) {
cWidth += 10;
cHeight += 10;
food[currentTarget].posX = 1000;
food[currentTarget].posY = 1000;
if(currentTarget < numFood-1) {
currentTarget++;
}
else {
currentTarget = 0;
}
}
}
if (cWidth >= 15 && cHeight >= 15){
cWidth -= .01;
cHeight -= .01;
}else{
cWidth = 15;
cHeight = 15;
}
}
void display() {
stroke(0);
fill(200,50,50);
noFill();
rectMode(CENTER); //comment out, corner can represent a head
rect(x,y,cWidth,cHeight);
}
}
class Food{
float sizeX;
float sizeY;
float posX;
float posY;
Food (float sx, float sy, float px, float py) {
sizeX = sx;
sizeY = sy;
posX = px;
posY = py;
}
void update (int tempMouseX, int tempMouseY) {
posX = tempMouseX;
posY = tempMouseY;
}
void display ()
{
fill (50,200,50);
ellipse (posX, posY, sizeX, sizeY);
}
}
void mousePressed(){
food[counter].update(mouseX, mouseY);
counter++;
// this is a bit of a kludge for demonstration purposes:
if(currentTarget == -1) {
currentTarget = 0;
}
if (counter >= numFood) {
counter = 0;
}
}
There are still a lot of improvements to make to this, but it at least demonstrates the principle: don't iterate over the whole food array, just target one at a time. As I said in the comments you really need a boolean to determine whether there are currently any food items on screen and better integration between adding food and setting the next target...
You'll quickly notice that the main problem with this as it stands is that once there aren't any food items on screen the creature goes off and gobbles those off-screen. Rather than simply moving the food objects offscreen, give them a boolean property to determine if they're 'active' (just check against this in the display method and don't draw them if they're not). When you increment 'currentTarget' you can check against the active state of the food - if it's not active simply increment currentTarget until it finds an active food item. Well that's one crude and slightly inefficient solution anyway...