0

I got an instance variable of type GRect

private GRect brick;

that I create several of, through iteration

private void makeBrickLineX(int x, int y, Color color)
{
    for (int i = 0; i < NBRICKS_PER_ROW; i++, 
                        x += BRICK_WIDTH + BRICK_SEP)                // Sets the x locations and separation between each brick.
    {                                                
        GRect brick  = new GRect (BRICK_WIDTH, BRICK_HEIGHT);
        brick.setFilled(true);
        brick.setColor(color);
        add(brick, x,y);
    }
}

The problem is, I am creating a "breakout" game, and I need to know when the ball has hit a brick. The code for it is simple, I use a method called getElementAt (balls x & y location) this all works. But there are several bricks, and calling remove on brick remove (brick). Only removes one brick in lowermost right corner, so these bricks must not share the same name? What can I do to make it work with all instances of "brick".

Robin Green
  • 32,079
  • 16
  • 104
  • 187
Tom Lilletveit
  • 1,872
  • 3
  • 31
  • 57
  • You should show more code, for example, how does this "add" method store the bricks? How does the `getElementAt()` function work, and are you sure remove is correct? – kurtzbot Sep 11 '12 at 21:43
  • This is unclear... what are `remove(brick)` and `add(brick, x, y)`? – Flavio Sep 11 '12 at 21:43
  • Am I right in assuming that `add(brick...)` simply draws the brick? It doesn't seem you're storing each brick in any kind of array/list/container. – twalberg Sep 11 '12 at 22:01

3 Answers3

0

What is the underlying data structure that you are using the store the bricks? What does remove(brick) do? Seems like an issue with hashCode() and/or equals() method if you are using a hashing data structure.

fo_x86
  • 2,583
  • 1
  • 30
  • 41
0

Each time you call

GRect brick = new GRect (BRICK_WIDTH, BRICK_HEIGHT)

you are creating a new variable 'brick' that gets set to a new GRect object. In Java, each variable must have a unique name, so you cannot have multiple GRect objects that are each named 'brick'.

To deal with this, each time you create an object called 'brick', Java is just overwriting the previous object named 'brick' with this second object named 'brick'. So the variable name 'brick' is only associated with the final brick object that you have created.

You are, presumably, still adding each brick object that you create to some data storage using your add() method. Can you show us the code for your add() method? Your add() method should most likely add each brick to some data structure, which you can then go through and delete all the bricks from directly.

Louisa
  • 861
  • 6
  • 5
0

You can remove all instances of GRect but you are just retaining the reference to the last instance named brick and removing it from your view. If you already store all bricks in an object already, you can remove them, like for example in the case of a List by giving a numeric index:

List allBricks = getAllBricks();
int removeIndex = getIndexForRemove();
allBricks.remove(removeIndex);

How do you read the location of the bricks when comparing against the location of the ball?

Pao
  • 843
  • 5
  • 17