-1

I am having a little issue in coding the collision detection for my java breakout game. The method that I am trying to work out collision on is this one:

    public void runAsSeparateThread()
        {
            final float S = 3; // Units to move (Speed)
            try
            {
                synchronized ( Model.class ) // Make thread safe
                {
                    GameObj       ball   = getBall();     // Ball in game
                    GameObj       bat    = getBat();      // Bat
                    List<GameObj> bricks = getBricks();   // Bricks
                }

                while (runGame)
                {
                    synchronized ( Model.class ) // Make thread safe
                    {
                        float x = ball.getX();  // Current x,y position
                        float y = ball.getY();

                        // Deal with possible edge of board hit
                        if (x >= W - B - BALL_SIZE)  ball.changeDirectionX();
                        if (x <= 0 + B            )  ball.changeDirectionX();
                        if (y >= H - B - BALL_SIZE)  // Bottom
                        { 
                            ball.changeDirectionY(); addToScore( HIT_BOTTOM ); 
                        }
                        if (y <= 0 + M            )  ball.changeDirectionY();

                        // As only a hit on the bat/ball is detected it is 
                        //  assumed to be on the top or bottom of the object.
                        // A hit on the left or right of the object
                        //  has an interesting affect

                        boolean hit = false;

                        if ( y <= bricks.getY() - (brickHeight/2)){
                            hit = true;
                        }
                        if ( y >= bricks.getY() - (brickHeight/2)){
                            hit = true;
                        }
                        if ( x < bricks.getX()){
                            hit = true;
                        }
                        if ( x > bricks.getX()){
                            hit = true;
                        }

                        if (hit)
                            ball.changeDirectionY();

                        if ( ball.hitBy(bat) )
                            ball.changeDirectionY();
                    }
                    modelChanged();      // Model changed refresh screen
                    Thread.sleep( fast ? 2 : 20 );
                    ball.moveX(S);  ball.moveY(S);
                }
            } catch (Exception e) 
            { 
                Debug.error("Model.runAsSeparateThread - Error\n%s", 
                    e.getMessage() );
            }
          }

However from the method above I am having an issue when I try to get the X & Y from the bricks. It keeps returning "Cannot find symbol - method getY()". However this method seems to work for the bat and ball but only throws this error for the bricks.

This error is thrown at this part of code:

boolean hit = false;

if ( y <= bricks.getY() - (brickHeight/2)){
    hit = true;
}
if ( y >= bricks.getY() - (brickHeight/2)){
    hit = true;
}
if ( x < bricks.getX()){
    hit = true;
}
if ( x > bricks.getX()){
    hit = true;
}

If anyone can help I would thoroughly appreciate it!

UPDATE

for ( int i = 0; i <= 60; i++ ){
                    GameObj brick1 = bricks.get(i);

                    if ( y <= brick1.getY() - (BRICK_HEIGHT/2)){
                        hit = true;
                        Debug.trace("BreakOut");
                    }
                    if ( y >= brick1.getY() - (BRICK_HEIGHT/2)){
                        hit = true;
                        Debug.trace("BreakOut");
                    }
                    if ( x < brick1.getX()){
                        hit = true;
                        Debug.trace("BreakOut");
                    }
                    if ( x > brick1.getX()){
                        hit = true;
                        Debug.trace("BreakOut");
                    }
                }
Vacation
  • 35
  • 7
  • Please create a [**minimal**, complete, and verifiable example](/help/mcve). Trim out all the code that's not relevant to the problem at hand. – John Kugelman Apr 08 '17 at 13:57
  • Please look at [What does a “Cannot find symbol” compilation error mean?](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – Hovercraft Full Of Eels Apr 08 '17 at 13:58
  • 1
    Your bricks variable is a `java.util.List` variable, so it makes sense that it doesn't have a `getY()` method. You need to get an object from the List, using `get(int i)` and call `getY()` on it. – Hovercraft Full Of Eels Apr 08 '17 at 14:00
  • I'm guessing that you would do this with a for loop, but most important -- read up on and understand how to use Lists such as ArrayLists. – Hovercraft Full Of Eels Apr 08 '17 at 14:01
  • I have done a for loop however I do not think it is working. I have updated the post with it. – Vacation Apr 08 '17 at 14:18

1 Answers1

2
private List<GameObj> bricks;  // The bricks

bricks is a list of many bricks. Which brick do you want the position of?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578