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

2012/2/21

Need AI help

1
2
THELAWL THELAWL

2012/2/21

#
nice! it works perfect now. could you maybe explain why i must do this to create a turtle?
danpost danpost

2012/2/21

#
You are not creating a turtle, you are just getting a reference to a turtle that is in the world. First we set 'tw' to be a reference to your world. Then we use 'getObjects(Turtle.class)' to return a list of all the turtles in your world. But we are not interested in a 'List' of turtle objects, we just want a reference to one turtle object; therefore 'get(0)' returns the first item off the list (as an 'Object'). Again, an object is not what we want, we want a 'Turtle', so we have to cast the returned Object to the Turtle class with '(Turtle)'. The variable 'turtle' now is a reference to a turtle (one that is already in your world). If you had more than one turtle in the world, you would not know which one was actually returned without closer inspection (but one of them would be referenced by the variable 'turtle'). The need for a reference to a turtle that is already in the world is paramount, as you cannot get a location of a turtle that is not in the world (one that is just created and not added to the world -- and then, it would not be the turtle you wanted, anyway). I noticed in your latest code post, that line 56 still has 'if (newTurtle.getX() >= getX())'. If that lines sill looks like that in your code, you need to change it to 'if (turtle.getX() >= getX())'
danpost danpost

2012/2/21

#
This does the same thing for the following method:
public void findTurtle()
{
    if (((Turtle) (getObjectsInRange(1000, Turtle.class).get(0))).getX() > getX()) move(3);
}
This avoids needing a reference to the world. By the way, this method takes care of computing distance for you (it uses a radial distance). So, if you only want to move toward the turtle if you are fairly close to it, just change the 1000 to the maximum distance.
snehalm.123 snehalm.123

2013/11/18

#
I've gone through the code, your AI implementation includes simulation of characters... what else...?? I'm developing a different game, and would like to know more, how I can include more of AI here..
You need to login to post a reply.
1
2