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

2012/2/26

getObjectsInRange help!

1
2
Y3llowMustang Y3llowMustang

2012/2/26

#
public void Find()
    {
        Player player = getObjectsInRange(Player.class);
        if(player != null)
        {
            setLocation(0, 0);
        }
    }
What is wrong with this guys
qnanqing qnanqing

2012/2/26

#
what is the error message?
Y3llowMustang Y3llowMustang

2012/2/26

#
incompatible types
danpost danpost

2012/2/26

#
Look in the Greenfoot API under the Actor class methods and check out what parameters are needed for the 'getObjectsInRange(..)' method. I will tell you this now: it takes more than one!
Y3llowMustang Y3llowMustang

2012/2/26

#
I have that figured out already, I'm still getting the same error, please help!
davmac davmac

2012/2/26

#
What does getObjectsInRange(...) return? Compare that with the type of variable you are assigning its return to. (Hint, use a cast, or a different variable type!)
Omniscience Omniscience

2012/2/27

#
A mere syntactical error; see the correct format:
 public void Find()  
  {  
        Player player = getObjectsInRange(int range, Player.class);  
        if(player != null)  
        {  
            setLocation(0, 0);  
        }  
    }  
Where int range is the radius around each image in which a predetermined effect will happen. Also, instead of writing if(player != null), try:
if (!getObjectsInRange(int range, Player.class).isEmpty()) { 
setLocation(0,0);
}
davmac davmac

2012/2/27

#
A mere syntactical error; see the correct format:
Err, no. As I've already mentioned, the return type is wrong. Specifically, getObjectsInRange returns a List - you cannot assign this to a variable of type Player.
Omniscience Omniscience

2012/2/27

#
1. Errr yes see the Greenfoot API, there was a syntax error seeing as he omitted the range paramater. I wasn't talking about the variable type. 2. Another reason why this is a syntax error is because he tried to assign a list to class (player). Rather, the correct syntax is to use "java.util.List variable = getObjectsInRange(range, Player.class);" as you mentioned.
davmac davmac

2012/2/28

#
Errr yes see the Greenfoot API
I'm perfectly familiar with the API. I was pointing out that what you posted was not correct, because it said the 'correct format' included:
       Player player = getObjectsInRange(int range, Player.class);
... which is wrong (because it tries to assign a reference of type List to a variable of type Player). I think we now agree on this? Also, danpost had already pointed out the parameter problem.
Y3llowMustang Y3llowMustang

2012/2/28

#
Since this returns a list is there a way I could filter everything out except the Player.class?
davmac davmac

2012/2/28

#
Since this returns a list is there a way I could filter everything out except the Player.class?
It returns a list containing Player instances. The list won't have anything in it other than Player instances (but it will be empty if there are no Player instances in range). You can check whether the list is empty using the 'isEmpty' method, as per recent post by Omniscience.
Y3llowMustang Y3llowMustang

2012/2/28

#
Sorry my bad, I meant like can I pick out the closest one and define it as a variable to use it? For example instead of teleporting the enemy to 0,0 it teleports the enemy to the Player.
danpost danpost

2012/2/29

#
Again, this is a bit vague. Let's see, you are in the Enemy class, trying to find a Player in range. But you want the closest Enemy class object to be the one to teleport to the Player (not neccessarily this particular instance of an Enemy class object). ?thinking? Maybe this process should be in the Player class (instead of the Enemy class), where you can list the enemies in range, and find the closest one to teleport with enemy.setLocation(getX(), getY());.
Y3llowMustang Y3llowMustang

2012/2/29

#
Good idea! Can you help me out with the coding please?
There are more replies on the next page.
1
2