This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
THELAWL
THELAWL wrote ...

2012/2/21

Need AI help

1
2
THELAWL THELAWL

2012/2/21

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
THELAWL THELAWL

2012/2/21

okay ive just worked on this single problem for 2 hours, and have found out that the SINGLE problem is that the getX() method is compeletly un-useable with respect to my turtle actor, it correctly sends information from any other actor, yet turtles dont seem to like getX() or getY(). the program works fine if i input normal numbers instead of GetX or Y, yet when i dont i get a error when i compile saying the turtle doesnt exist. when it clearly does, i can see it on the screen.
Builderboy2005 Builderboy2005

2012/2/21

The problem is that you haven't added the turtle into the world. getX() returns the error you are receiving when the object exists, but has not been added into the world yet. Is it possible you have created a separate turtle that you havent added into the world, and you are testing that instead? It might be a good idea to post a bit more of your code so we can potentially see where you might have gone wrong.
THELAWL THELAWL

2012/2/21

import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage public class TurtleWorld extends World { /** * Create the turtle world. Our world has a size * of 560x460 cells, where every cell is just 1 pixel. */ public TurtleWorld() { super(600, 480, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Counter counter = new Counter(); addObject(counter, 542, 458); Turtle turtle = new Turtle(counter); addObject(turtle, 300, 200); Snake snake = new Snake(); addObject(snake, 444, 181); Lettuce lettuce = new Lettuce(); addObject(lettuce, 139, 121); Lettuce lettuce2 = new Lettuce(); addObject(lettuce2, 125, 247); Lettuce lettuce3 = new Lettuce(); addObject(lettuce3, 278, 391); Lettuce lettuce4 = new Lettuce(); addObject(lettuce4, 410, 402); Lettuce lettuce5 = new Lettuce(); addObject(lettuce5, 503, 319); Lettuce lettuce6 = new Lettuce(); addObject(lettuce6, 328, 49); Lettuce lettuce7 = new Lettuce(); addObject(lettuce7, 200, 197); Lettuce lettuce8 = new Lettuce(); addObject(lettuce8, 155, 401); Lettuce lettuce9 = new Lettuce(); addObject(lettuce9, 373, 280); Lettuce lettuce10 = new Lettuce(); addObject(lettuce10, 26, 127); Lettuce lettuce11 = new Lettuce(); addObject(lettuce11, 473, 111); Lettuce lettuce12 = new Lettuce(); addObject(lettuce12, 572, 80); Lettuce lettuce13 = new Lettuce(); addObject(lettuce13, 355, 121); Lettuce lettuce14 = new Lettuce(); addObject(lettuce14, 85, 404); Lettuce lettuce15 = new Lettuce(); addObject(lettuce15, 165, 333); Lettuce lettuce16 = new Lettuce(); addObject(lettuce16, 206, 57); Snake snake2 = new Snake(); addObject(snake2, 121, 65); Snake snake3 = new Snake(); addObject(snake3, 65, 244); Snake snake4 = new Snake(); addObject(snake4, 334, 458); Bug bug = new Bug(); addObject(bug, 289, 96); Bug bug2 = new Bug(); addObject(bug2, 393, 374); Bug bug3 = new Bug(); addObject(bug3, 131, 341); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.Random; /** * Write a description of class Snake here. * * @author (your name) * @version (a version number or a date) */ public class Snake extends Animal { /** * Act - do whatever the Snake wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ Turtle newTurtle = new Turtle(); public void act() { findTurtle(); turnAtEdge(); moveRandom(); eat(); } public void turnAtEdge() { if(atWorldEdge()) { turn(7); } } public void moveRandom() { if (Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(40)-20); } } public void eat() { if (canSee(Turtle.class)) { eat(Turtle.class); } } public void findTurtle() { if (newTurtle.locationX() > getX()) move(3); } } import greenfoot.*; public class Turtle extends Animal { private int Points = 0; private Counter counter; public Turtle(Counter pointCounter) { counter = pointCounter; } public Turtle() { } public void act() { controls(); eat(); } public void controls() { if( Greenfoot.isKeyDown("W") ) move(3); if( Greenfoot.isKeyDown("D") ) turn(5); if( Greenfoot.isKeyDown("A") ) turn(-5); } public void eat() { if (canSee(Lettuce.class)) { eat(Lettuce.class); counter.add(5); if (counter.getValue() >= 100) { gameOver(); } } if (canSee(Bug.class)) { eat(Bug.class); counter.add(20); createNewBug(); if (counter.getValue() >= 100) { gameOver(); } } } private void createNewBug() { Bug newBug = new Bug(); World world = getWorld(); int x = world.getHeight(); int y = world.getWidth(); world.addObject(newBug, Greenfoot.getRandomNumber(x) , Greenfoot.getRandomNumber(y) ); } public int totalPoints() { int x = Points; return x; } public void gameOver() { Greenfoot.stop(); } public int locationX() { int x = getX(); return x; } public int locationY() { int y = getY(); return y; } } 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.findTurtleX(Snake.java:55) at __SHELL0.run(__SHELL0.java:57) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at greenfoot.localdebugger.LocalDebugger$QueuedExecution.run(LocalDebugger.java:267) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:411) at greenfoot.core.Simulation.maybePause(Simulation.java:269) at greenfoot.core.Simulation.runContent(Simulation.java:201) 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 Turtle.locationX(Turtle.java:88) at Snake.findTurtle(Snake.java:55) 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 Turtle.locationX(Turtle.java:88) at Snake.findTurtle(Snake.java:55) 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 Turtle.locationX(Turtle.java:88) at Snake.findTurtle(Snake.java:55) 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) error occurs when i run the game. hope this helps
Builderboy2005 Builderboy2005

2012/2/21

Yep, you create newTurtle, but at no point do you add newTurtle into the world. Now the question is, do you want each snake to create a new turtle and insert that into the world, or was your goal to make the snake be able to find turtles that are already in the world?
THELAWL THELAWL

2012/2/21

find turtles already in the world, the turtle is the player
danpost danpost

2012/2/21

In the Snake class, remove the line 'Turtle newTurtle = new Turtle();'. Then, also in the Snake class, change this method:
public void findTurtle()
{
    TurtleWorld tw = (TurtleWorld) getWorld();
    Turtle turtle = tw.getObjects(Turtle.class).get(0);
    if (turtle.getX() > getX())  move(3);
}
You will also get an error in your Turtle class in your 'addNewBug()' method, unless you get a reference to the world as above.
THELAWL THELAWL

2012/2/21

highligets Turtle turtle = tw.getObjects(Turtle.class).get(0); and says incompatibe types the (0) is red
danpost danpost

2012/2/21

That is the number zero in parenthesis. All numeric values show up in red in the code format. If that is not the problem, was there any more to the error message?
danpost danpost

2012/2/21

Try: Turtle turtle = (Turtle) (tw.getObjects(Turtle.class).get(0)); in line 4
THELAWL THELAWL

2012/2/21

nope didnt work still says incompatible types
Builderboy2005 Builderboy2005

2012/2/21

Could you post the entire error? Does it say what it is expecting or what is found?
danpost danpost

2012/2/21

What does your code look like now (and please use the format tags for code this time).
THELAWL THELAWL

2012/2/21

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

import java.util.Random;

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Snake extends Animal
{
    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */     
    
    public void act() 
    {
        findTurtle();
        turnAtEdge();
        moveRandom();
        eat();
    }
    
    public void turnAtEdge()
    {
        if(atWorldEdge())
        {
            turn(7);
        }
    }
    
    public void moveRandom()
    {
        if (Greenfoot.getRandomNumber(100) < 10)
        {
            turn(Greenfoot.getRandomNumber(40)-20);    
        }
    }
    
    public void eat()
    {
        if (canSee(Turtle.class))
        {
            eat(Turtle.class);
        }
    }
    
    public void findTurtle()
    {
        TurtleWorld tw = (TurtleWorld) getWorld();
        Turtle turtle = tw.getObjects(Turtle.class).get(0);
        Turtle turtle = (Turtle) (tw.getObjects(Turtle.class).get(0));
        
        if (newTurtle.locationX() > getX())
            move(3);
    }
    
}
error is on the source code -incompatible types.. no ther erroros
danpost danpost

2012/2/21

You did not make the adjust I proposed. Change line 53 to
Turtle turtle = (Turtle) (tw.getObjects(Turtle.class).get(0));
I tested it and no errors came up. EDITED: Sorry, I see you added the line, but did not remove the old line -- remove line 53.
You need to login to post a reply
1
2