0

I'm currently trying to instantiate 1 row of bricks before moving on to 5. Before implementing the for loop I made sure I could instantiate 1 brick on the screen, exactly where i want it. No problem there. The problem comes when looping over the length of the constant COLS (10) defined before the main function. For some reason my bricks bunch up with no spacing. Can you see anything glitchy in my function?

// Brick width and height
#define BRICK_W 30
#define BRICK_H 7.5


void initBricks(GWindow window)
{
    // set initial brick coordinates
    int x_coord = 5;
    int y_coord = 10;

    // Create 10 columns of bricks
    for (int i = 0; i < COLS; i++)
    {
        // create a brick
        GRect brick = newGRect(x_coord, y_coord, BRICK_W, BRICK_H);
        setFilled(brick, true);
        setColor(brick, "RED");
        add(window, brick);

        // increment x coordinate for spacing
        x_coord = x_coord + 10;
    }
}

For some reason my bricks just come out bunched together with no spacing towards the upper left of the window. Any thoughts?

1

x_coord is incrementing by 10 each time, yet your brick width is set to 30. Looks like you might have mixed your x's and y's up!

x_coord = x_coord + 10;

Instead of 10, you will need to increment by 30 plus whatever you have calculated as the gap.

4
  • wow i can't believe i didn't even think about that. I actually changed the increment to a larger number before reading your post and had better results but didn't quite understand why until now. Thanks! – jakewies May 25 '15 at 18:19
  • the problem now is nesting this for loop to run 5 times (for rows). I increment the y coordinate outside the inner for loop but still can't seem to get more than 1 row showing up in the window. – jakewies May 25 '15 at 18:20
  • Post the code as it is now... – Phil M May 25 '15 at 18:22
  • Hey @jake_wies Currently I am facing the exact same problen u mentioned in the comment... How did u solve that issue of nested looping? – Sai Charan Nivarthi Sep 18 '15 at 10:20

You must log in to answer this question.

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