Homework 3 (Breakout) FAQ

Q: Why are my bricks the wrong size?
A: Are you using int sizes? Maybe you should use double for the highest precision. Also, are you sure your math is right? If there are 10 bricks per row, there are 9 gaps between them and 2 more gaps around the edges. Also, are you sure you're using the right values for the width/height of the overall game window? You should use getWidth() and getHeight() for this, not the constants like APPLICATION_WIDTH.
Q: What color should I make the bricks if there are more than 10 rows?
A: This is totally up to you, as long as the code doesn't crash and the bricks are given some color. For example, you could start over the rainbow back at its starting color of red, or you could just make all rows >= 10 equal to the tenth row's color.
Q: Why can't I see the paddle?
A: Are you sure you're using the right values for the width/height of the overall game window? You should use getWidth() and getHeight() for this, not the constants like APPLICATION_WIDTH. Try resizing the window larger to see if the paddle shows up.
Q: How do I keep track of the ball's velocity?
A: We suggest that you use fields (private instance variables) for this. For example:
private double vx;
private double vy;
Q: How do I make a random initial velocity for the ball?
A: As mentioned in the spec, you should choose random real numbers between 1.0 - 3.0 in the positive or negative direction. One way to do this would be to write code such as the following:
RandomGenerator rgen = new RandomGenerator(); 
vx = rgen.nextDouble(VELOCITY_MIN, VELOCITY_MAX);
if (rgen.nextBoolean(0.5)) {
    vx = -vx;   // randomly use -1.0 .. -3.0 half the time
}

// don't just say rgen.nextDouble(-3.0, 3.0) ... why not?
Q: Why does my program stall with red "NullPointerException" text in the Eclipse console?
A: You will see a NullPointerException if you try to call any methods on a variable that stores null. This would be the case if you have a field that is supposed to store a graphical object, but you never set its value.
Q: How do I tell whether my ball should bounce off of a brick horizontally or vertically?
A: For simplicity, when it bounces off of a brick, just always make it bounce vertically.
Q: Why does my ball get "stuck" jittering back and forth inside a wall or inside the paddle?
A: Your logic about how to "bounce" is slightly off. It is over-simplistic to just write code to reverse the velocity. If you hit, say, the left wall, it should now always have an x-velocity to make it go to the right. Don't accidentally reverse it twice, which will lead to the "jitter" effect.
Q: How do I know what the ball collided with?
A: As mentioned in the spec, you can use the getElementAt method to ask what graphical object (if any) occupies a given x/y pixel position. For example, if you store your game's paddle in a field named paddle, you could check to see if the ball collided with the paddle by writing code such as:
GObject collider = getElementAt(x, y);
if (collider == paddle) {
    ...
}

The above code is oversimplified, though, because you are actually supposed to check for each of the four points around the edges of the ball. You might want to write a method named getCollidingObject or similar that checks all four of these points and returns the object that the ball collided with, or null if there is none. You could use it roughly as follows:

GObject collider = getCollidingObject();
if (collider == paddle) {
    ...
}
Q: Why does my ball sometimes bounce off the bottom wall without it ending the player's turn?
A: This is a funny bug that some students run into. Perhaps you have left the last ball from the previous turn sitting down there just off the bottom of the screen, and the current ball is bouncing off of that. Make sure to remove any old ball from a prior turn and/or reuse it on the next turn, rather than just leaving it sitting there on the window.
Q: How many fields is it okay to have? Can ______ be a field?
A: We don't want to answer exactly what fields are okay or not okay. That is part of the assignment, for you to decide what fields are appropriate. You should follow the guidelines we have taught you for deciding that. If a value must be used through multiple methods over time and it can't be solved using a parameter or return, that is a good candidate for a field.
This document and its content are copyright © Marty Stepp, 2014. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.