Search code examples
vb.netmessagebox

Multiple message boxes appear instead of one


I am designing a Breakout style game in VB and am having some minor problems with my code.

I am checking for collisions between the ball and the 4 sides of the form. So, as the ball collides with the bottom of the form, the game is meant to display a message box saying "You Lost!" with the Retry and Cancel buttons.

This is what I have coded under my Timer1_Tick event:

'check bottom of screen 
If PictureBox_ball.Top >= 403 Then
    '403 is the Y-coordinate of a horizontal line I have implemented 
    MsgBox("You Lost!", MessageBoxButtons.RetryCancel)
End If

However, when I run this code, the game displays multiple message boxes (around 25 of the same ones) instead of displaying one. And it doesn't stop there.

As the ball moves downwards and hits the bottom of the screen, the ball stops moving, displays a bunch of message boxes and then continues to move downwards and out of the screen.

How would I be able to fix this?


Solution

  • This is what I have coded under my Timer1_Tick event:

    Stop your timer before you call out your msgBox
    WHY ??

    Because the the timer is always ticking, and as it ticks, it displays your msgBox.

    Just try it, I had the same problem before on my projects.