You can only return one value, but you could make that value an array. For example:
private GObject[] getCollidingObjects() {
GObject[] ret = new GObject[2];
ret[0] = ...;
ret[1] = ...;
return ret;
}
Btw, when you start reusing the same expression multiple times in the same method, you should think about introducing a local variable for clarity. For example, consider this instead of your original code:
private GObject getCollidingObject(){
int x = ball.getX();
int y = ball.getY();
if (getElementAt(x, y) != null) {
return getElementAt(x, y);
}
if (getElementAt(x + BALL_RADIUS * 2, y) != null) {
return getElementAt(x + BALL_RADIUS * 2, y);
}
if (getElementAt(x, y + BALL_RADIUS * 2) != null) {
return getElementAt(x, y + BALL_RADIUS * 2);
}
if (getElementAt(x + BALL_RADIUS * 2, y + BALL_RADIUS * 2) != null) {
return getElementAt(x + BALL_RADIUS * 2, y + BALL_RADIUS * 2);
}
return null;
}
(You could do the same for x + BALL_RADIUS * 2 and y + BALL_RADIUS * 2 as well.)
You might also consider something like this:
private GObject getCollidingObject(){
int x = ball.getX();
int y = ball.getY();
return getFirstNonNull(getElementAt(x, y),
getElementAt(x + BALL_RADIUS * 2, y),
getElementAt(x, y + BALL_RADIUS * 2),
getElementAt(x + BALL_RADIUS * 2, y + BALL_RADIUS * 2));
}
private static getFirstNonNull(GObject... objects) {
for (GObject x : objects) {
if (x != null) {
return x;
}
}
return null;
}
(In C# there's a nicer way of doing this with the null coalescing operator, but never mind...)