Im working on a project for scool and i need an enemy to be able to run towards the hero
(maybe with diffrent speeds). I have no idea how to do it since im bad at coding...
Please help.
// with a field private int rotation;
// the following act format public void act() { setRotation(rotation); // all moving codes here // // the following at end of act rotation = getRotation(); setRotation(0); }
java.util.List playerList = getWorld().getObjects(Player.class) if ( ! playerList.isEmpty()) { Actor player = playerList.get(0); turnTowards(player.getX(), player.getY()); move(3); }
java.util.List playerList = getObjectsInRange(200, Player.class); // adjust range as needed
public void act() { follow(); } public void follow() { trackX(); trackY(); } public void trackX() { { World world; world=getWorld(); Object obj=world.getObjects(InsertNameHere.class).get(0); InsertNameHere doc=(InsertNameHere) obj; int InsertNameHereX=doc.getX(); getWorld().showText(""+InsertNameHereX,50,10); if (getX() < InsertNameHereX) move(2); if (getX() > InsertNameHereX) move(-2); } } public void trackY() { { World world; world=getWorld(); Object obj=world.getObjects(InsertNameHere.class).get(0); InsertNameHere doc=(InsertNameHere) obj; int InsertNameHereY=doc.getY(); getWorld().showText(""+InsertNameHereY,150,10); if (getY() > InsertNameHereY) setLocation(getX(),getY()-2); if (getY() < InsertNameHereY) setLocation(getX(),getY()+2); } }