I'm making an Arkanoid in Visual Basic as a way to practice. At the moment the Form1 only contains a Label called "ship", and through a timer and keyboard events I control its movement.
So far everything works apparently fine, until I add a RadioButton that will act as a "ball". From this moment on, the ship no longer moves.
I attach code in case someone can help me.
Public Class Form1
Dim flechaIzq As Boolean = False
Dim flechaDer As Boolean = False
'Movimiento
Dim comenzar As Boolean = False
Dim izquierda As Boolean = True
Dim arriba As Boolean = True
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Width = 800
Me.Height = 500
'bola.Location = New Point(nave.Left + (nave.Width / 2) - (bola.Width / 4), nave.Location.Y - bola.Height)
reloj.Enabled = True
reloj.Interval = 30
End Sub
Private Sub reloj_Tick(sender As Object, e As EventArgs) Handles reloj.Tick
If flechaDer Then
If nave.Left < ClientSize.Width - nave.Width Then
nave.Location = New Point(nave.Location.X + 10, nave.Location.Y)
End If
ElseIf flechaIzq Then
If nave.Left > 0 Then
nave.Location = New Point(nave.Location.X - 10, nave.Location.Y)
End If
End If
'If comenzar Then
'mover()
'End If
End Sub
Private Sub mover()
If arriba Then
'bola.Location = New Point(bola.Location.X, bola.Location.Y + 5)
Else
'bola.Location = New Point(bola.Location.X, bola.Location.Y - 5)
End If
If izquierda Then
'bola.Location = New Point(bola.Location.X + 5, bola.Location.Y)
Else
'bola.Location = New Point(bola.Location.X - 5, bola.Location.Y)
End If
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Left Then
flechaIzq = True
End If
If e.KeyCode = Keys.Right Then
flechaDer = True
End If
'If e.KeyCode = Keys.Space Then
'comenzar = True
'End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Left Then
flechaIzq = False
ElseIf e.KeyCode = Keys.Right Then
flechaDer = False
End If
End Sub
End Class
As you can see, at this point, all code that somehow references the "ball" is commented out. Just adding the RadioButton to the form makes the "ship" not move with the Left/Right keys
All the best!
Your problem is that when you add a radiobutton inside your form, then now every key is processed by said control, since it has the focus.
The label cannot have the focus, so every key pressed goes to the form, which is the one with the focus.
To fix this, you have several options: