
Originally Posted by
BryceTheCoder
Here is a function I like called goodDistance(int, int, int, int, int);
However, if you have a PI base you probably have it already inside your Player class.
Here is the function:
Code:
public boolean goodDistance(int objectX, int objectY, int playerX,
int playerY, int distance) {
for (int i = 0; i <= distance; i++) {
for (int j = 0; j <= distance; j++) {
if ((objectX + i) == playerX
&& ((objectY + j) == playerY
|| (objectY - j) == playerY || objectY == playerY)) {
return true;
} else if ((objectX - i) == playerX
&& ((objectY + j) == playerY
|| (objectY - j) == playerY || objectY == playerY)) {
return true;
} else if (objectX == playerX
&& ((objectY + j) == playerY
|| (objectY - j) == playerY || objectY == playerY)) {
return true;
}
}
}
return false;
}
This will do a simple if statement check to see if the player is within "DistanceYouDesire" tiles with the object.
Hope I helped

Couldn't you just replace that whole method with 1 if statement?
Code:
public boolean goodDistance(int objectX, int objectY, int playerX, int playerY, int distance) {
if (((playerX + playerY) - (objectX + objectY)) <= distance && ((objectX + objectY) - (playerX + playerY) <= distance)) {
return true;
} else {
return false;
}
}
Of course this might be a bit cleaner:
Code:
public boolean goodDistance(int objectX, int objectY, int playerX, int playerY, int distance) {
int objSum = objectX + objectY;
int playerSum = playerX + playerY;
if (playerSum - objSum <= distance && objSum - playerSum <= distance) {
return true;
} else {
return false;
}
}
maybe ternary?
Code:
public boolean goodDistance(int objectX, int objectY, int playerX, int playerY, int distance) {
int firstDif = (playerX + playerY) - (objectX + objectY);
int secDif = (objectX + objectY) - (playerX + playerY);
boolean result = (firstDif <= distance && secDif <= distance) ? true : false;
return result;
}
These are untested though, may not work lol.