is it possible for the following element to return multiple items in one call ( i.e. two GRects)
private GObject getColidingObject(){
if(getElementAt(ball.getX(), ball.getY()) != null){
return getElementAt(ball.getX(), ball.getY());
}else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY()) != null){
return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY());
}else if(getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2) != null){
return getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2);
}else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2) != null){
return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2);
}else{
return null;
}
}
You can only return one value, but you could make that value an array. For example:
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:
(You could do the same for
x + BALL_RADIUS * 2andy + BALL_RADIUS * 2as well.)You might also consider something like this:
(In C# there’s a nicer way of doing this with the null coalescing operator, but never mind…)