This site requires JavaScript, please enable it in your browser!
Greenfoot back
accucore
accucore wrote ...

2014/4/27

Enemy following Player

accucore accucore

2014/4/27

#
the code that is here is for the enemy to follow the player the only problem is that the enemy starts to rotate in a way that i dont want it to, does anyone know of a way to follow the player without changing the enemies rotation //to attack player if he´s somewhere around the enemy public void atackPlayer() { if(!getObjectsInRange(200, Player.class).isEmpty()) { Actor player = (Actor) getObjectsInRange(200, Player.class).get(0); if(player != null) { followPlayer(); } } } //this is the code for the enemy to follow the player public void followPlayer() { turnTowards(Player.xPlayer , Player.yPlayer); /*this is the problem over here, is there any other way of figuring out the players position without comprimising the rotation of the enemy*/ move(1); Actor player = (Actor) getOneIntersectingObject(Player.class); if(player != null) { getWorld().removeObject(player); } } thanks to anyone that responds
accucore accucore

2014/4/27

#
i forget to explain that the enemies movement is more linear you could say, the enemies can only go up, down ,left or right , but im trying with the code on top, that ounce the player gets into the enemies field that it will go after the player no matter what direction he goes even in the diagonal until the player gets out of that field, of course if the enemies "touches" the player the player dies.
danpost danpost

2014/4/27

#
You can just reset the rotation of the Enemy back to zero after moving toward the player. You should probably pass the player to the 'followPlayer' method, since you already have a reference to it:
1
followPlayer(player);
Then, your method would be:
1
2
3
4
5
6
7
private void followPlayer(Actor player)
{
    turnTowards(player.getX(), player.getY());
    move(1);
    setRotation(0);
    if (intersects(player)) getWorld().removeObject(player);
}
accucore accucore

2014/4/28

#
That works just the way i want it to, thank you danpost :)
You need to login to post a reply.