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

2012/4/26

move

tylers tylers

2012/4/26

#
how do you have it so that if a fish sees a peise of food it swims to it.
danpost danpost

2012/4/26

#
Use the actor class method 'getObjectsInRange(int radius, Class class)' which returns a list of Class type objects within the radius given. (You could find the closest piece, and have the fish goto it). There are several discussions that deal with moving toward another object, search for them and check them out.
tylers tylers

2012/4/26

#
ive got this so far cant do the rest please help.
public void find()
    {
java.util.List zom =  getObjectsInRange(50,food.class);
int z = zom.size();
        if( z>=1)
        {
            java.util.List foodList = getNeighbours(100, true, food.class); 
            turnTowards(foodList.get(0));
            
        }
            
        
    }
danpost danpost

2012/4/26

#
public void find()
{
    // find the closest piece of food within 50 of 'this'
    int distance = 1;
    while (distance < 51 && getObjectsInRange(distance, food.class).isEmpty()) distance++;
    if (distance == 51) return; // no food found in range
    turnTowards(getObjectsInRange(distance, food.class).get(0)); // closest piece found
}
You do not even have to import java.util.List for this to work.
tylers tylers

2012/4/27

#
i got aN error message say on line 7 requited int,int found object
danpost danpost

2012/4/27

#
I coded that line with the same syntax as your line 8 above. Check the turnTowards(...) method declaration to see what type parameters it uses.
You need to login to post a reply.