Hey All,
I am working on a project and I'm running into a few issues. The first issue is my fish will quit moving once I hit the up or down arrow. I can spin the fish fine, but when I use the up or down arrow is seems to quit working. I'm not getting any compile errors.
The second issue is when the fish reaches a rock in the world, the fish quits moving all together. What I want to happen is the rock should an obstacle for the fish. So the fish has to go around the rock and cannot go through the rock. My code is below. Please look at it and let me know what I'm doing wrong? Thanks for your help!
public class Fish extends Animal
{
int speed = 0; //Starting speed of the fish
final int MAX_SPEED = 1; //Max speed the fish can go
final int DELAY = 1; //The delay of the fish's speed
/**
*Act - the Fish hunts for flies in the water world.
*This 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
lookForFly(); //look for the Fly method
keys(); //The Keys method
}
/**
* Check whether a control key on the keyboard has been pressed.
* If it has, react accordingly.
*/
public void keys()
{
if(Greenfoot.isKeyDown("right")) //Moves fish right
{
if(getIntersectingObjects(Rock.class).isEmpty())
{
turn(3); //Location of the fish in the World
}
}
if(Greenfoot.isKeyDown("left")) //Moves fish left
{
if(getIntersectingObjects(Rock.class).isEmpty())
{
turn(-3); //Location of the fish in the World
}
}
if (Greenfoot.isKeyDown("up") && speed > -MAX_SPEED * DELAY) //Moves the fish up with different speeds
{
if(getIntersectingObjects(Rock.class).isEmpty())
{
move(speed++); //Creates the different speeds of the fish
}
}
if (Greenfoot.isKeyDown("down") && speed > -MAX_SPEED * DELAY) //Moves the fish down with different speeds
{
if(getIntersectingObjects(Rock.class).isEmpty())
{
move(speed--); //Creates the different speeds of the fish
}
}
}
/**
* Check whether the fish has stumbled upon a Fly.
* If it have, eat it. If not, do nothing.
*/
public void lookForFly()
{
if (canSee(Fly.class)) //can the Fish see the Fly
{
eat(Fly.class); //if Fly is found, then crab eats it
Greenfoot.playSound("slurp.wav"); // .wav file is played when Fly is eaten
}
}
