So in my game when ever you have the right or left arrow key down its stopping the program and I dont know why.
the code for the actors that move is:
And the code for this specific actor is:
Thanks
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Mover extends Actor { private static final int acceleration = 2; // down (gravity) private static final int speed = 7; // running speed (sideways) private int vSpeed = 0; // current vertical speed /** * Move a bit to the right. */ public void moveRight() { setLocation ( getX() + speed, getY() ); } /** * Move a bit to the left. */ public void moveLeft() { setLocation ( getX() - speed, getY() ); } /** * Return true if we're on firm ground that we can stand on. */ public boolean onGround() { Object under = getOneObjectAtOffset(0, getImage().getHeight()/2-8, null); return under != null; } /** * Set a speed for a vertical movement. Positive values go down, negative values * go up. */ public void setVSpeed(int speed) { vSpeed = speed; } /** * Apply gravity and fall downwards until we hit ground or the bottom of the screen. */ public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; if ( atBottom() ) gameEnd(); } /** * Return true if we're at the bottom of the screen. */ private boolean atBottom() { return getY() >= getWorld().getHeight() - 2; } /** * End this game (that is: stop the simuation). */ private void gameEnd() { Greenfoot.stop(); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) // (World, Actor, GreenfootImage, and Greenfoot) /** * the evolution animal needs to go from one side to the other */ public class Animal1 extends Mover { private static final int jumpStrength = 16; private int level; private int jumped = 0; // count how often we've jumped public Animal1() { level = 1; } public void act() { checkKeys(); checkFall(); checkNextLevel(); } private void checkKeys() { if (Greenfoot.isKeyDown("left") ) { setImage("pengu-left.png"); moveLeft(); } if (Greenfoot.isKeyDown("right") ) { setImage("pengu-right.png"); moveRight(); } if (Greenfoot.isKeyDown("space") ) { if (onGround()) jump(); } } private void jump() { setVSpeed(-jumpStrength); fall(); jumped++; } private void checkFall() { if (onGround()) { setVSpeed(0); } else { fall(); } } /** * Check whether we should go to the next level, and if yes, start the next level. */ private void checkNextLevel() { if (getX() == getWorld().getWidth()-1) { if (level == 1) { level = 2; getWorld().removeObject(this); } else { level = 1; getWorld().removeObject(this); } } } }