So I have two yes inside a character that follow the enemy below them, but they would fly outside of the face if the enemy was outside of the face, so I added barriers to stop that from happening. However, it doesn't work and I can't figure out why.
Here's the code:
Thanks for your time! :)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class eyes here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class eyes extends TheFace
{
/**
* Act - do whatever the eyes wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
follow();
checkForBarrierOne();
}
public void follow(){
startenemy startenemy = new startenemy();
List objectslookingfor = getWorld().getObjects(startenemy.class);
if (objectslookingfor.size() > 0){
int enemyx = ((startenemy) getWorld().getObjects(startenemy.class).get(0)).getX();
if(checkForBarrierOne()==false||checkForBarrierTwo()==false||checkForBarrierThree()==false||checkForBarrierFour()==false){
if(getX()<enemyx){
setLocation(getX()+1,getY());
}
if(getX()>enemyx){
setLocation(getX()-1,getY());
}
}
else if(checkForBarrierOne()==true&&getX()<enemyx){
if(getX()<enemyx){
setLocation(getX()+1,getY());
}
}
else if(checkForBarrierTwo()==true&&getX()>enemyx){
if(getX()<enemyx){
setLocation(getX()-1,getY());
}
}
else if(checkForBarrierThree()==true&&getX()<enemyx){
if(getX()<enemyx){
setLocation(getX()+1,getY());
}
}
else if(checkForBarrierFour()==true&&getX()>enemyx){
if(getX()<enemyx){
setLocation(getX()-1,getY());
}
}
}
}
private boolean checkForBarrierOne(){
boolean check;
if(getOneIntersectingObject(barrierOne.class)!=null){
check = true;
}
else{check=false;}
return check;
}
private boolean checkForBarrierTwo(){
boolean check;
if(getOneIntersectingObject(barrierTwo.class)!=null){
check = true;
}
else{check=false;}
return check;
}
private boolean checkForBarrierThree(){
boolean check;
if(getOneIntersectingObject(barrierThree.class)!=null){
check = true;
}
else{check=false;}
return check;
}
private boolean checkForBarrierFour(){
boolean check;
if(getOneIntersectingObject(barrierFour.class)!=null){
check = true;
}
else{check=false;}
return check;
}
}
