0

I'm trying to get 10 bricks in the columns and 4 rows of bricks, however all the bricks are on the same line. Can someone please help? if you need more info let me know. Thanks

void initBricks(GWindow window) { // TODO int brickXLocation = 0; int brickYLocation = 0;

// Adds a new row
for(int i = 0; i < ROWS; i++)
{ 
    // adds bricks horizontally
    for(int j = 0; j < COLS; j++)
    {
        GRect brick = newGRect( brickXLocation + 2, brickYLocation, BRICK_WIDTH, BRICK_HEIGHT);
        setFilled(brick, true);
        setColor(brick, "BLUE");
        add(window, brick);
        brickXLocation = (brickXLocation + 4) + BRICK_WIDTH;  
    }
        brickYLocation = (brickYLocation + 4) + BRICK_HEIGHT;
}

}Bricks on one row only

0

So you're updating brickYlocation at the end of the loop, but you're reinitializing/redeclaring in the very first line of the loop. What do you think happens? It ends up with the value for the first row!

Now, if you had actually declared and initialized it before the start of the loop, that probably wouldn't be a problem.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Well, that's because you have exactly the opposite problem with brickXlocation. You don't reset it to the beginning of each new row with each loop. You're actually creating the bricks, but they're outside the boundary of the window. If you could see them, the original version would have been one long row of bricks. In the revised version, they'd look like a staircase. If you reset the x value outside of the j loop but inside the i loop, it should work fine.
    – Cliff B
    Nov 25 '15 at 6:48
  • Thank you very much. Its like my mind blocked out the fact I needed to reset brickXLocation. Such a simple issue tripped me up for so many hours. Thanks.
    – Widdakay
    Nov 25 '15 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .