I have some fruit that randomly choose a direction to move in (up,down,left,right) and whe n they reach the edge the are supposed to turn around and go the other direction. but they only turn at the edge at the bottom and left of the screen then get stuck when they reach the top or the right. how do i fix? This is one of the fruits code
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Apple here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Apple extends Actor
{
int d=Greenfoot.getRandomNumber(4);
int fruitClicked=0;
/**
* Act - do whatever the Apple wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
movement();
removeOnClick();
}
private void movement(){
if (d==1){
if(1==1){
setLocation(getX()+5,getY());
turnAtEdge();
}
}
if(d==2){
if(1==1){
setLocation(getX()-5,getY());
turnAtEdge();
}
}
if (d==3){
if(1==1){
setLocation(getX(),getY()-5);
turnAtEdge();
}
}
if(d==4){
if(1==1){
setLocation(getX(),getY()+5);
turnAtEdge();
}
}
}
public void turnAtEdge(){
if(isAtEdge()){
if(d==1){
d=2;
}
if(d==2){
d=1;
}
if(d==3){
d=4;
}
if(d==4){
d=3;
}
}
}
public void youWin(){
if (fruitClicked==16){
getWorld().showText("You Win" ,400, 300);
Greenfoot.stop();
}
}
public void removeOnClick()
{
if (Greenfoot.mouseClicked(this)) {
World world;
world = getWorld();
world.removeObject(this);
return;
}
if (Greenfoot.mouseClicked(this)) {
fruitClicked++;
}
}
}

