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

2013/10/5

help with AI

manish123 manish123

2013/10/5

#
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?
danpost danpost

2013/10/5

#
In your 'closestPoint' method you are creating a new Points object on one line, and on the next one you are trying to get where it is in the world using getX() and getY(), when it has not been placed in the world. If you want to get the location of the closest point, have 'Available' return the Points object it found, if any, instead of just returning a true/false value of whether it found one or not. Then use the location of the point returned from 'Available', if the returned object is not 'null', to turn toward.
manish123 manish123

2013/10/6

#
oh ok so how would i do that? Instead of saying return true; would i say return; im kinda confused at how to return the point object from available
danpost danpost

2013/10/6

#
You give the class of the object to return. So, instead of 'boolean', you would use 'Points'. And instead of returning 'true' or 'false', you would return a Points object or 'null'. By convention, method names should always begin with a lowercase letter, so I changed 'Available' to 'available'.
public Points available()
{
    int radius = 5;
    if (getObjectsInRange(radius, Points.class).isEmpty()) return null;
    return (Points)getObjectsInRange(radius, Points.class).get(0);
}

public void closestPoint()
{
    Points p = available();
    if (p != null) turnTowards(p.getX(), p.getY());
}
You need to login to post a reply.