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

2017/4/25

getObjectsInRange is always empty?

Robot_Face Robot_Face

2017/4/25

#
The following code should have my actor circling around a Center as long as it's in range, and moving in a straight line otherwise:
public void act()
{
    List<Center> centers = getObjectsInRange(1200, Center.class);
    for(Center c : centers)
    {
        turnTowards(c.getX(), c.getY());
        turn(90);
    }
    move(1);
}
For some reason, it is never turning, even when it's extremely close to or even on top of a Center. I tried getObjectsInRange(Integer.MAX_VALUE, Center.class), which should pick up the center in any reasonable situation, but it doesn't. I'm afraid I might be overlooking something obvious.
danpost danpost

2017/4/25

#
The code given is not the reason for the behavior you are getting. Please post your World subclass codes and your Center class codes.
Super_Hippo Super_Hippo

2017/4/25

#
Could it be that the object is turning (the image), but it only keeps moving straight? Does your actor extend the SmoothMover class? If not, did you try changing the 1 in line 9 to something higher? It's just a guess.
Robot_Face Robot_Face

2017/4/25

#
Actually, I figured it out. When I just create a Center in the GUI and press Run, it is not seen by the method getObjectsInRange. I have to either Save the World or manually create it in the world's prepare method. It's not the most convenient, but at least now I know it's not a problem with my code.
You need to login to post a reply.