i just started Greenfoot today and was wondering how i could implent an AI into my modified "trick-the-Turtle" game that would make the enemy snakes begin to follow the playr turtle once the player turtle comes to close to a snake.
the problem im having is that i am creating a varible "distance" that defines the distance between a snake and the player turtle.
currently in have made some methods
this is in snake class:
- "newTurtle" is an instance varile declared before act method
public void moveTowardsTurtle()
{
if (newTurtle.getX() > getX() )
{
move(3);
}
}
it compiles fine, yet when i run the game i instantly get this
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
at greenfoot.Actor.failIfNotInWorld(Actor.java:656)
at greenfoot.Actor.getX(Actor.java:157)
at Snake.move2(Snake.java:53)
at Snake.act(Snake.java:23)
at greenfoot.core.Simulation.actActor(Simulation.java:507)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:470)
at greenfoot.core.Simulation.runContent(Simulation.java:204)
at greenfoot.core.Simulation.run(Simulation.java:194)
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
at greenfoot.Actor.failIfNotInWorld(Actor.java:656)
at greenfoot.Actor.getX(Actor.java:157)
at Snake.move2(Snake.java:53)
at Snake.act(Snake.java:23)
at greenfoot.core.Simulation.actActor(Simulation.java:507)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:470)
at greenfoot.core.Simulation.runContent(Simulation.java:204)
at greenfoot.core.Simulation.run(Simulation.java:194)
should i not be useing "getX" for this type of AI, is there a better way?
any help is appreicated