Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can only return one value, but you <em>could</em> make that value an array. For example:</p> <pre><code>private GObject[] getCollidingObjects() { GObject[] ret = new GObject[2]; ret[0] = ...; ret[1] = ...; return ret; } </code></pre> <p>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:</p> <pre><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; } </code></pre> <p>(You could do the same for <code>x + BALL_RADIUS * 2</code> and <code>y + BALL_RADIUS * 2</code> as well.)</p> <p>You might also consider something like this:</p> <pre><code>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; } </code></pre> <p>(In C# there's a nicer way of doing this with the null coalescing operator, but never mind...)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload