I need some help please. I'm having trouble making the player stop moving when the player touches a fence object. When the player touches the fence object initially the method bottomTouching() returns true however my first if/else statement doesn't seem to stop the player from moving forward after touching the fence. However once the player touches the fence objects, walks away, then touches it again, only then will the player be prevented from moving.
public void move(){ Actor back = (Actor)getWorld().getObjects(background.class).get(0); if(Greenfoot.isKeyDown("up")){ animate(); setImage(user[0][currentFrame]); if(background.moving()){ setLocation(getX(), getY()); } else if(!bottomTouching()){ setLocation(getX(), getY()-2); } direction = 0; } else if(Greenfoot.isKeyDown("right")){ animate(); setImage(user[1][currentFrame]); if(background.moving()){ setLocation(getX(), getY()); } else{ setLocation(getX()+2, getY()); } direction = 1; } else if(Greenfoot.isKeyDown("down")){ animate(); setImage(user[2][currentFrame]); if(background.moving()){ setLocation(getX(), getY()); } else{ setLocation(getX(), getY()+2); } direction = 2; } else if(Greenfoot.isKeyDown("left")){ animate(); setImage(user[3][currentFrame]); if(background.moving()){ setLocation(getX(), getY()); } else if(!sideTouching()){ setLocation(getX()-2, getY()); } direction = 3; } else{ currentFrame = 0; if(direction == 0){ setImage(user[0][currentFrame]); } else if(direction == 1){ setImage(user[1][currentFrame]); } else if(direction == 2){ setImage(user[2][currentFrame]); } else{ setImage(user[3][currentFrame]); } } }
public boolean bottomTouching() { Actor fenceCheck = getOneObjectAtOffset(0, getImage().getHeight()/2-20, fence.class); return fenceCheck == null; }