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");
}
}