How do you make something move right and left randomly?
// instance fields private int speed = 1; // assign appropriate value private int direction = 1; // '1' for right; '-1' for left; and '0' for no movement // within act or a method it calls move(speed*direction);
int min_x = 0; int max_x = 600; int x = Greenfoot.getRandomNumber(max_x - min_x) + min_x; public void act() { if(getX() == x) x = Greenfoot.getRandomNumber(max_x - min_x) + min_x; int speed = 5; if(x >= getX()+speed) setLocation(getX()+speed, getY()); else if(x <= getX()-speed) setLocation(getX-speed, getY()); else if(x > getX() || x < getX()) setLocation(x, getY()); }