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

2016/5/16

getObjectInRange

Laurence Laurence

2016/5/16

#
I've been trying to code something that makes it so when an actor is within range then another actor appears but this has proved challenging (On my part). I've been trying different ways to do it but can never pin point it. Thanks in advance.
danpost danpost

2016/5/16

#
Let me call the object to be shown a Proximity object (of class Proximity). First in the class of the actor, set up a field to hold a Proximity object;
1
private Proximity proxObj = null;
Then, in the constructor of the class, create one and retain it in the field:
1
proxObj = new Proximity();
Finally, in the act method, or a method it calls:
1
2
3
4
5
if ((proxObj.getWorld() == null) != getObjectsInRange(< range >, OtherActor.class).isEmpty())
{
    if (proxObj.getWorld() == null) getWorld().addObject(proxObj, < x, y >);
    else getWorld().removeObject(proxObj);
}
Laurence Laurence

2016/5/16

#
So if I had an actor called 'Player.class' how would that morph into the code, I'm still a bit confused.
danpost danpost

2016/5/16

#
Laurence wrote...
So if I had an actor called 'Player.class' how would that morph into the code, I'm still a bit confused.
Which one is the player, the actor that checks for the near object or the object being checked for as being near?
Laurence Laurence

2016/5/16

#
The one that is being checked
danpost danpost

2016/5/16

#
Laurence wrote...
The one that is being checked
If the class of object being checked for to be in range of the actor is the player (of the Player class), then replace 'OtherActor.class' with 'Player.class' above.
Laurence Laurence

2016/5/17

#
And the actor checking for the near object would be? I'm getting it so far, I just want to cement my knowledge on it since I'm going to be using it heavily.
danpost danpost

2016/5/17

#
Laurence wrote...
And the actor checking for the near object would be? I'm getting it so far, I just want to cement my knowledge on it since I'm going to be using it heavily.
The actor of the class the code will be located in (the one that holds the Proximity object and also adds and removes it from the world). Really now, all you gave us to work with until just recently (with Player) was 'actor' and 'another actor' (one object was not actually mentioned -- just assumed). Of those three (from your initial post), it would be the one not mentioned (it is that which the Player object is in range of). The Player object was determined to be 'actor' and I gave the Proxiimity object its name and is the 'another actor'.
Laurence Laurence

2016/5/18

#
I know it's kind of vague with the information I have supplied you with, but it's really all I need to know. I kind of only need to know how to use getObjectInRange which is why I did not give any code. I do apologise and I do thank you for your support.
Orko Orko

2016/5/18

#
It's easy, here it is getObjectsInRange(int radius, java.lang.Class<A> cls)
danpost danpost

2016/5/19

#
To be specific, the Actor class API says this:
protected java.util.List getObjectsInRange(int radius, java.lang.Class cls) Return all objects within range 'radius' around this object.
'protected' means you can only call the method from an Actor subclass (or the Actor class which you cannot edit); 'java.util.List' means it returns a List object (btw, the method never returns 'null'); 'getObjectsInRange' is the name of the method; 'int radius' is the first parameter which should be a positive integer indicating how far the search extends; 'java.lang.Class cls' is the second parameter which is the type of Actor objects to look for (giving 'null' will search for all types);
You need to login to post a reply.