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

2013/11/2

Up and down arrows increase and decrease speed

LugNut29 LugNut29

2013/11/2

#
Hi All, I need help with making the up and down arrows increase and decrease the distance the fish travels, effectively changing its speed. How do I do this? Here's what I have thus far: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The fish tries to eat the fly, while being chaces by a Dolphin. * @author Thomas Dozer * @version 10 November 2013 */ public class Fish extends Animal { /** * Act - do whatever the Fish wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveFish(); //Key stroke method lookForFly(); //look for the Fly method } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ private void moveFish() { if(Greenfoot.isKeyDown("left")) { turn(-4); } if(Greenfoot.isKeyDown("right")) { turn(4); } if(Greenfoot.isKeyDown("up")) { move(5); } if(Greenfoot.isKeyDown("down")) { move(-5); } } /** * Check whether we have stumbled upon a Fly. * If we 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 } } }
danpost danpost

2013/11/2

#
Right now, you have the speed hard-coded with '5's. The first thing you need to do is set up an instance field so that the speed can be varied. Then, maybe, adding 'get' and 'set' methods (and/or possibly an 'add' method) for it.
LugNut29 LugNut29

2013/11/3

#
Ok, I change it around a little bit. I found another post you commented on and I cannot get it to work with the example you gave. this is the example you gave: // instance variables whose values can be adjusted int speed = 0; final int MAX_SPEED = 5; final int DELAY = 20; // must be greater than zero // within act or a method it calls if (Greenfoot.isKeyDown("up") && speed < MAX_SPEED * DELAY) speed++; if (Greenfoot.isKeyDown("down") && speed > -MAX_SPEED * DELAY) speed--; When I try to add something like this, the up and down won't work at all. It complies fine and gives no errors, but my fish will not move. Here's what I did: public class Fish extends Animal { private int Speed = 5; int speed = 0; final int MAX_SPEED = 5; final int DELAY = 20; /** *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 { setLocation(getX()+Speed, getY()); //Location of the fish in the World } if(Greenfoot.isKeyDown("left")) //Moves fish left { setLocation(getX()-Speed, getY()); //Location of the fish in the World } if (Greenfoot.isKeyDown("up") && speed < MAX_SPEED * DELAY) speed++; if (Greenfoot.isKeyDown("down") && speed > -MAX_SPEED * DELAY) speed--; } /** * 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 } }
danpost danpost

2013/11/3

#
You (for some reason that I cannot figure) have two fields for the speed of the fish ('Speed' and 'speed'). Remove 'private int Speed = 5;' and change all remaining 'Speed' to 'speed'.
LugNut29 LugNut29

2013/11/4

#
Ha..Yeah I'm very, very green to programming. I have been playing around with this a little and it seems to work about 90% of the time. When the fish gets in a corner, it seems that the up and down keys quit working. I'm sorry if I'm asking a lot of questions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Fish extends Animal
{
   int speed = 0;
   final int MAX_SPEED = 5;
   final int DELAY = 20;
   /**
    *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
       {
           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
       }
       if (Greenfoot.isKeyDown("up") && speed > -MAX_SPEED * DELAY)
       {
           move(speed++);
       }
       if (Greenfoot.isKeyDown("down") && speed > -MAX_SPEED * DELAY)
       {
           move(speed--);
       }
   }
You need to login to post a reply.