Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Stanford CS106a breakout assignment

    Hi

    I'm doing the CS106a course online. I'm currently nearing the end of the Breakout assignment, where the exercise is to code a game of breakout. It's all working fine except for this piece of code;

    Java Code:
    private GObject getCollidingObject() {
    			GObject collision = getElementAt(ball.getX(), ball.getY());
    			if (collision == null) {
    				getElementAt(ball.getX(), ball.getY() + (2 * BALL_RADIUS));
    			}
    			if (collision == null) {
    				getElementAt(ball.getX() + (2 * BALL_RADIUS), ball.getY());
    			}
    			if (collision == null) {
    				getElementAt(ball.getX() + (2 * BALL_RADIUS), ball.getY() + (2 * BALL_RADIUS));
    			}
    			return(collision);
    		}
    I'm trying to get the getCollidingObject method to check the four corners around the ball for collisions. The getElementAt method returns either the object or null. So my thinking is that if collision == null should work as a test before checking the next corner, to find if there's an object at all four corners. However, this code, when its running, appears to be only checking the first corner. What have I done wrong?

    Thanks! R

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Stanford CS106a breakout assignment

    What do you plan on doing with the element returned from getElementAt method inside the if statements?

  3. #3
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Stanford CS106a breakout assignment

    Quote Originally Posted by Junky View Post
    What do you plan on doing with the element returned from getElementAt method inside the if statements?
    Oops, just did this, which works. Thanks!

    Java Code:
    private GObject getCollidingObject() {
    			GObject collision = getElementAt(ball.getX(), ball.getY());
    			if (collision != null) {
    				return(collision);
    			}
    			GObject collision2 = getElementAt(ball.getX(), ball.getY() + BALL_RADIUS);
    			if (collision2 != null) {
    				return(collision2);
    			}
    			GObject collision3 = getElementAt(ball.getX() + BALL_RADIUS, ball.getY());
    			if (collision3 != null) {
    				return(collision3);
    			}
    			GObject collision4 = getElementAt(ball.getX() + BALL_RADIUS, ball.getY() + BALL_RADIUS);
    			if (collision4 != null) {
    				return(collision4);
    			}
    			return(null);
    		}

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Stanford CS106a breakout assignment

    You don't need to keep declaring collision references.
    Just reuse the original one.
    eg (pseudoish code)
    Java Code:
    GObject collision = getElementAt(ball.getX(), ball.getY());
    if (!= null) return;
    collision = getElementAt(ball.getX(), ball.getY() + BALL_RADIUS);
    if (!= null) return;
    etc etc
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Stanford CS106a GraphicsHeirarchy
    By Newbieprogrammer in forum New To Java
    Replies: 0
    Last Post: 07-15-2012, 07:37 PM
  2. CheckerBoardKarel - Stanford CS106A Assignment 1
    By mattdotmac in forum New To Java
    Replies: 6
    Last Post: 07-15-2012, 01:58 PM
  3. Stanford cs106a
    By D.good in forum Introductions
    Replies: 1
    Last Post: 02-04-2012, 06:18 PM
  4. Breakout Assignment from CS106a
    By tech8462 in forum New To Java
    Replies: 13
    Last Post: 10-09-2011, 03:38 AM
  5. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 04:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •