if i change the else if to an if it does destroy both blocks like it should
This is normal, if you keep "else if" then you will not check for collision on the blue block when you already found a colliding red block.
moveByY = -moveByY;
This is were your problem lies.
You check if the ball collides with the red block and you change the direction of the ball.
Then (if not using 'else if') you will change it again when colliding with the blue block. This is why it keeps going.
My advice would be the following: (I didn't think about this long, so there might be a better solution)
- Use 'if' and not 'else if', because you need to check collision with both blocks.
- Use a bool CollisionDetected, which is set to false in the beginning of the function. If the ball collides with the red block then you make it true. When the ball collides with the blue block then you don't invert the direction of the ball when CollisionDetected was already true.