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

2013/2/6

Help? getObjectsInRange..

Draymothisk Draymothisk

2013/2/6

#
I have never used this method before, but I need to use it for the game I am creating. I need the enemy in my game to detect when the Hero (Guy) is within its shooting range. I have looked through some other posts on this site, but I still cannot fully grasp how to use it. So far, I have this in my enemy: public boolean isGuyInRange() { Guy Guy; Guy = (Guy)getObjectsInRange(1200, Guy.class); return Guy != null; if(Guy != null) { System.out.println("Working"); } } This compiles an error of "unreachable statement", and I have not been able to get past this point. Any help?
Busch2207 Busch2207

2013/2/6

#
If there's a 'return' in your method, the method will end, when it reaches it, so it won't get to "if(Guy != null) ". There's also another problem: The method 'getObjectsInRange' will not return a single object! It returns a List of all objects, that are in that range! To handle with the List, you have to write: 'import java.util.List;' above your Source Code. Then in the method write this:
List<Guy> list_guy_=getObjectsInRange(1200,Guy.class);
for(Guy guy_:list_guy_)
{
   // Write whatever you want to do with every Object in the List
   // Here you have an object called guy_ that reprensents one object after the other in the List
}
Here's also an overview over all methods in the 'List'-class: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html
Draymothisk Draymothisk

2013/2/6

#
Thanks :) It worked, but now there is one minor issue. 1. I have a compiler pop-up (doesn't seem to affect the game, but it is there) that says "Compiler warnings: Recompile with Xlint: unchecked for details".
Draymothisk Draymothisk

2013/2/6

#
Yea, I have not had to deal with Arrays or lists yet, so I'm completely in the dark with them. :(
danpost danpost

2013/2/6

#
Real simply, without having to use 'import java.util.List;'
public boolean isGuyInRange()
{
    return !getObjectsInRange(1200, Guy.class).isEmpty();
}
Draymothisk Draymothisk

2013/2/7

#
I have one more problem now. Is there a way for my enemy to detect Guy (the hero) on a specific side with "getObjectsInRange"? Since this method detects in a radius, I can't think of a way for my enemy to say, "hey, he's to my right". Any help or ideas?
Busch2207 Busch2207

2013/2/7

#
You can get the angle to an object by:
    public double getDegreesTo(double x,double y)
    {
        return Math.toDegrees(Math.atan2(y-getY(),x-getX()));
    }
danpost danpost

2013/2/7

#
You will need to get the location of the Guy object and compare it to the location of this enemy detecting him.
if (isGuyInRange())
{
    Actor guy = (Actor) getObjectsInRange(1200,  Guy.class).get(0);
    int x = guy.getX();
    int y = guy.getY();

    // now you can use either
    double angleToGuy = getDegreesTo(x, y);
    if (angleToGuy >90 && angleToGuy<270) // Guy is to left
    if (angleToGuy > 270 || angleToGuy < 90) // Guy is to right

    // or you can use
    if (x < getX()) // Guy is to left
    if (x > getX()) // Guy is to right
}
Draymothisk Draymothisk

2013/2/7

#
Thanks both of you! Look forward to my work soon up; working hard.
You need to login to post a reply.