hi, i am currently working on my Ai on my game and ran into some trouble. I want to make my AI to search for points and get the closest one near it this is what i have so far:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
* Write a description of class blueBlock here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class redBlock extends PlatformMover
{
/**
* Act - do whatever the blueBlock wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int pointsTaken = 0;
public redBlock()
{
}
public void act()
{
moveRandom();
closestPoint();
}
public void moveRandom()
{
if(Greenfoot.getRandomNumber(100) < 10)
{
turn(Greenfoot.getRandomNumber(90) -45);
move(2);
}
}
public void Takepoint()
{
if(getOneIntersectingObject(Points.class)!= null)
{
World world = getWorld();
world.removeObject(this);
}
}
public boolean Available()
{
int radius = 5;
if(getObjectsInRange(radius, Points.class) != null)
{
return true;
}
else
{
return false;
}
}
public void closestPoint()
{
if(Available() == true)
{
Points p = new Points();
turnTowards(p.getX(), p.getY());
}
}
}
But when i run it, it gives me this exception:
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:663)
at greenfoot.Actor.getX(Actor.java:157)
at redBlock.closestPoint(redBlock.java:63)
at redBlock.act(redBlock.java:26)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
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:663)
at greenfoot.Actor.getX(Actor.java:157)
at redBlock.closestPoint(redBlock.java:63)
at redBlock.act(redBlock.java:26)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
The thing is my point is already in the world but this exception is telling me its not, anybody know whats going on?