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

2016/3/13

Need help moving worms

Alucard Alucard

2016/3/13

#
Hello, I need to give the worms a chance to survive! Have the worms respond to the “up” and “down” arrow keys on the keyboard by moving the worms left (for “up”) or right (for “down”) a random number of steps between 0 and 3. This is the following code but it is not responding. I need some help please. public class Worm extends Actor { public void act() { if ( isAtEdge() ) { turn(3); } if ( Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(60) - 18); } move(3); } public void checkKeypress() { if (Greenfoot.isKeyLeft("up")) { turn(-3); } if (Greenfoot.isKeyRigh("down")) { turn(3); } } }
danpost danpost

2016/3/13

#
I know that you must be getting some error messages. Which one are you currently getting and what line does it appear to occur on?
Toly Toly

2016/3/13

#
What you have is public void checkKeypress() { if (Greenfoot.isKeyLeft("up")) { turn(-3); } if (Greenfoot.isKeyRigh("down")) { turn(3); } } } It should be public void checkKeypress() { if (Greenfoot.isKeyDown("up")) { turn(-3); } if (Greenfoot.isKeyDown("down")) { turn(3); } } } Greenfoot.isKeyDown just means if the key is being pressed or not, not which key is being pressed.
You need to login to post a reply.