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

2013/11/7

Compiles, but does not work correctly. Need help.

LugNut29 LugNut29

2013/11/7

#
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
       }
   }
Zamoht Zamoht

2013/11/7

#
I changed the code and here is the result. Please tell me if this is not what you wanted:
public class Fish extends Animal  
{  
    int speed = 0; //Starting speed of the fish  
    int delay = 0;
    final int MAX_SPEED = 5; //Max speed the fish can go  
    final int DELAY = 8;

    /** 
     * Check whether a control key on the keyboard has been pressed. 
     * If it has, react accordingly. 
     */  
    public void keys()  
    {  
        if (delay > 0) delay--;

        if(Greenfoot.isKeyDown("right")) //Moves fish right  
        {  
            turn(3); //Location of the fish in the World   
        }  
        if(Greenfoot.isKeyDown("left")) //Moves fish left  
        {  
            turn(-3); //Location of the fish in the World  
        }  
        boolean keyDown = false;
        int tempDelay = delay;
        if (Greenfoot.isKeyDown("up")) //Moves the fish up with different speeds  
        {  
            keyDown = !keyDown;
            if (speed < MAX_SPEED && delay == 0) {
                speed++;
                tempDelay+=DELAY;
            }
        }  
        if (Greenfoot.isKeyDown("down"))  //Moves the fish down with different speeds  
        {  
            keyDown = !keyDown;
            if (speed > -MAX_SPEED && delay == 0) {
                speed--;
                tempDelay-=DELAY;
            }
        }  
        delay = Math.abs(tempDelay);
        move(speed);
        if (getOneIntersectingObject(Rock.class) != null) {
            move(-speed);
        }
        if (!keyDown) {
            speed = 0;
        }
    }  
}
LugNut29 LugNut29

2013/11/7

#
Hey Zamoht, Thanks for the update. I added your changes and the fish won't move at all now. Any ideas?
Zamoht Zamoht

2013/11/7

#
You added keys() to the act method?
LugNut29 LugNut29

2013/11/7

#
Ahhh...I didn't even see that. I forgot to add the act() method back in. That's exactly want I was trying to do. Thank you again for your help!
You need to login to post a reply.