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

2018/3/6

How to use this

hosfeldli hosfeldli

2018/3/6

#
I have this--> getObjectsInRange( 50, Survivor.class); I'm a beginner though and I don't really know what to do with it.
CubeGamer6 CubeGamer6

2018/3/6

#
The getObjectsInRange() method returns a List object. A java.util.List must be imported first and you can use the List.get(int) method to get stuff that is within the list Usually you would use a loop to do something with all things in a list:
List<Survivor> list = getObjectsInRange(50, Survivor.class);

for(int i = 0; i < list.size(); i++) {
    Survivor s = list.get(i);
    // todo: do things with the Survivor object
}
Hope this helps PS: I strongly reccommend looking at the Greenfoot API and the javadocs
danpost danpost

2018/3/6

#
It is true that the 'getObjectsInRange' method returns a List object. That does not, however, necessarily mean you need to import the java.util.List class. Only if you specifically create a variable that is of that type is it necessary. All @hosfeldli needs to do is see if the list is empty and get a reference to the actor in the list, if not:
if (!getObjectsInRange(50, Survivor.class).isEmpty()) // if list is not empty
{
    Actor survivor = (Actor)getObjectsInRange(50, Survivor.class).get(0);
    turnToward(survivor.getX(), survivor.getY());
}
The only thing that might be improved is the range, as '50' seems like quite a short range.
danpost danpost

2018/3/6

#
It could also be coded like this without having to import the class:
java.util.List survivors = getObjectsInRange(Survivor.class);
if (!survivors.isEmpty())
{
    Actor survivor = (Actor)survivors.get(0);
    turnToward(survivor.getX(), survivor.getY());
}
hosfeldli hosfeldli

2018/3/6

#
What is wrong with this
private Actor getOneObjectInFront(Class c)
    {
        GreenfootImage myImage = getImage();
        int distanceToFront = myImage.getHeight();
        int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
        int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
        return (getOneObjectAtOffset(xOffset, yOffset, c));
    }
hosfeldli hosfeldli

2018/3/6

#
it says xOffset and the yOffset are incompatible java language
hosfeldli hosfeldli

2018/3/6

#
I did use a youtube video to learn how to do this so if it isn't understandable then I can't help too much. I know what is happening, but that is all
hosfeldli hosfeldli

2018/3/6

#
Thank you for the help up top, sorry I have another one.
danpost danpost

2018/3/6

#
hosfeldli wrote...
I did use a youtube video to learn how to do this so if it isn't understandable then I can't help too much. I know what is happening, but that is all
You could reference the youtube video here (supply its URL or link it here).
hosfeldli hosfeldli

2018/3/7

#
Here is the link & https://www.youtube.com/watch?v=BoXn_Fx9898
danpost danpost

2018/3/7

#
Try the following return line:
return ((Actor)getOneObjectAtOffset(xOffset, yOffset, c));
hosfeldli hosfeldli

2018/3/7

#
That fixed it thank you
You need to login to post a reply.