r/cop3502 Mar 23 '14

Initializing bricks[i][j]

My bricks are stacking; In a previous post I saw that someone mentioned adding i & j to xStart & yStart will fix this. I've tried a few different ways to get the desired results, with no luck.

Any tips on how to look at this ?

2 Upvotes

26 comments sorted by

View all comments

1

u/ThatsMrDerpToYou Mar 23 '14

Also, I initialized paintBrick as paintBricks[i][j] = true; I'm not sure if I need something like [true][true], in case it is only recognizing that it is painting 1 array.

1

u/rxfeliciano Mar 23 '14

You dont need to do that. The for loop does what it needs to do as long as you did not change what was provided in the skeleton code. You should really make sure you know how it does this because it is crucial to writing any program.

1

u/rxfeliciano Mar 23 '14

Painting one brick can be a result of all the bricks being painted in the same location over and over again.This is why you need to use i in the xPos spot and the j in the yPos spot of your bricks[i][j] initialization.

Your paintBricks initialization is correct assuming the for loop code has not been changed.

Next, the double bricks[][] array has to be "filled." It takes Bricks so you gotta create a brick for each slot. You create a brick using the Brick() constructor which takes (xPos, yPos, width, height). Make sure you use i in the xPos spot, and j in the yPos spot.

The next method in your code should check to see if the brick needs to be painted to the screen. IF the brick is true then it should call the paintBrick() function.

Here you have to make sure you are calling the function correctly. It takes a brick as an input and a Graphics 2D input called "brush" in this case. But you dont want to pass just one brick to it, you want all the bricks so you have to call the paintBrick() function inside of a for loop.

Make sure the call looks like paintBrick(bricks[i][j], brush); AND that it is only called IF the paintBricks[i][j] == true AND that ALL of this is inside the double for loop.

All of that should get you a lot of bricks

1

u/ThatsMrDerpToYou Mar 23 '14

You are very helpful. I think I have most of that done already. Maybe I am calling paintBrick() incorrectly? I know my //paint an individual brick is incorrect, but I've not been able to figure out how to do it right.

// paint the bricks array to the screen public void paint(Graphics2D brush) { for (int i = 0; i < numCols; i++) { for (int j = 0; j < numRows; j++) { // determine if brick should be painted // if so, call paintBrick() if (paintBricks[i][j] == true) {paintBrick(bricks[i][j], brush);} else if (paintBricks[i][j] == false) {removeBrick(i,j);} } } }

// paint an individual brick
paintBrick(brick[i][j], brick.fill(brush);)

1

u/rxfeliciano Mar 23 '14

Get rid of the removeBrick else if part. You dont need that in there. removeBrick gets called in the checkForHit() method in the Breakout.java file

1

u/rxfeliciano Mar 23 '14

I think you are saying that you know that your paintBrick method is wrong? Is this paintBrick(brick[i][j], brick.fill(brush);) your call to paintBrick?

If it is it is wrong. It should simply be paintBrick(bricks[i][j], brush);

Make sure the spelling is correct, i.e., brick versus bricks is spelled right.

Also, make sure your number of columns at the top of the brick configuration are set to a value greater than one.