Hey folks,
I am trying to get interactions done in a little fisicia-based game of mine. Goal is a method/function, that returns the nearest type of objectType X (passed as an argument) in a given interaction distance (say the player can only interact with objects in a +/- 30/30 square box around him).
This method looks currently like this:
Object fetchNearestObject(String objectType) { //return coordinates of nearest object of type 'objectType' inside action radius
ArrayList nearestObjects = new ArrayList();
while (nearestObjects.get(0).getClass().getSimpleName() != objectType ) {
for (float extentX=0; extentX<=actionRadius; extentX++) {
for (float extentY=0; extentY<=actionRadius; extentY++) {
ArrayList temp1 = new ArrayList();
ArrayList temp2 = new ArrayList();
temp1 = world.getBodies((player.xPos + extentX), (player.yPos + extentY), true);
temp2 = world.getBodies((player.xPos - extentX), (player.yPos - extentY), true);
if (temp1.size() != 0) {
for (int i=0; i<temp1.size(); i++) {
if (temp1.get(i).getClass().getSimpleName() == objectType) {
nearestObjects.add(temp1.get(i));
println("Found " + temp1.get(i));
}
}
}
if (temp2.size() != 0) {
for (int i=0; i<temp2.size(); i++) {
if (temp2.get(i).getClass().getSimpleName() == objectType) {
nearestObjects.add(temp2.get(i));
println("Found " + temp2.get(i));
}
}
}
}
}
}
return nearestObjects.get(0);
As you can see, it returns an object type (am I doing this right? )
Now I’m using this method call in other interaction functions, e.g. in the player method void climb
void climb(String direction) {
if (fetchNearestObject("Ladder").getClass().getSimpleName() == "Ladder") {// check if ladder is in range and center player according to ladder base/top
if (fetchNearestObject("Ladder").topAnchor.get(1) >= this.yPos-actionRadius) {
this.xPos = fetchNearestObject("Ladder").topAnchor.get(0);
} else if (fetchNearestObject("Ladder").bottomAnchor.get(1) >= this.yPos+actionRadius) {
this.xPos = fetchNearestObject("Ladder").bottomAnchor.get(0);
}
if (direction == "up") {
yPos = yPos++;
} else if (direction == "down") {
yPos = yPos--;
}
}
}
But it seems like JAVA doesn’t like this way of object passing (I am far from being a skilled JAVA developer; I know just the basics). The error raised is basically global variable topAnchor does not exist
, same goes for any method like the getX()-function inherited to the “Ladder” class by fisica’s FBody class.
Hope you can help me with that - how can I access the methods and local variables of an object returned by my fetchNearestObject()
function?
Kind regards from Germany
Yannik