Hi!
I've got the following problem with coding an enemie in my Greenfoot game. The enemie is supposed to follow the player. I tried this code to make it possible:
Player class:
MyWord class:
The "other code" does not influence this code in any way. When I compile this, the enemie is following the player, but not as I want it. I already created the same game with a drag and drop "coding" program called Scratch.
There I just chose the "Turn Towards" code and the "Move" code to make the enemie follow the player. Let me show you the difference in the behavior of the enemie between the Scratch and the Greenfoot version:
Scratch version:
or http://www.mediafire.com/view/crps5dysp7fmxss/Scratch.gif
Greenfoot version:
or http://www.mediafire.com/view/tggz4cd14x8n32t/Greenfoot.gif
I hope the difference between those two is visible. I'd like the enemie to follow the player as it does in the Scratch version. How do I have to change the code for that?
Thank you for your answers!
Player player = ((MyWorld)getWorld()).player;
turnTowards(player.getX(), player.getY());
move(1);other code
public Player player;
public MyWorld()
{
super(1200, 900, 1);
player = new Player();
other code
addObject(player, 110, 100);
other code
}
or http://www.mediafire.com/view/crps5dysp7fmxss/Scratch.gif
Greenfoot version:
or http://www.mediafire.com/view/tggz4cd14x8n32t/Greenfoot.gif
I hope the difference between those two is visible. I'd like the enemie to follow the player as it does in the Scratch version. How do I have to change the code for that?
Thank you for your answers!
