I am working on a brick breaker game for Android. Currently, I am using an ArrayList for the bricks. I need to remove each one individually from the list and the screen upon collision with the ball. Currently, when the ball collides with the very last brick in the array (bottom right corner), half of the array gets removed (every other brick). Upon colliding with it again, it removes a few more bricks randomly, but none of this is even close to what I need.
Here is my code. brick
is an object within the Bricks ArrayList.
for (int i = 0; i <= bricks.size(); i++) {
if (RectF.intersects(brick.rect, ball.rect)) {
bricks.remove(bricks.get(i));
ball.dy = -ball.dy;
score += 10;
}
}
i <= bricks.size()
in thefor
seems wrong. Use<
instead of<=
? - Pangbricks.remove()
while iteratingbricks
in ascending order seems a bad idea. - Pangbrick.rect
instead ofbricks.get(i).rect
in yourif
line? Is theif
line supposed to be not directly dependent oni
? - Pang