ayuda con las teclas!!!
estoy haciendo un juego para dos players, ambos utilizan el teclado para desplazarse a la vez por la pantalla, pero existe un problemilla y es que cuando el player1 esta pulsando las teclas para desplazarse el player2 no puede hacer nada y viceversa. Que puedo hacer para evitarlo??
Gracias
Gracias
DeberÃas explicar como lo estás haciendo. De todas
formas te pongu un ejemplo, abre un formulario e
inserta 2 controles shape y un timer con Interval=50,
además al form le pones KeyPreview=True y ScaleMode=3-Pixels.
Después pon este código:
Private Const izquierda As Byte = 1
Private Const derecha As Byte = 2
Private Const arriba As Byte = 4
Private Const abajo As Byte = 8
Dim direccion1 As Byte, direccion2 As Byte
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
If (direccion1 And izquierda) = 0 Then
direccion1 = direccion1 + izquierda
End If
Case vbKeyRight
If (direccion1 And derecha) = 0 Then
direccion1 = direccion1 + derecha
End If
Case vbKeyUp
If (direccion1 And arriba) = 0 Then
direccion1 = direccion1 + arriba
End If
Case vbKeyDown
If (direccion1 And abajo) = 0 Then
direccion1 = direccion1 + abajo
End If
Case vbKeyA
If (direccion2 And izquierda) = 0 Then
direccion2 = direccion2 + izquierda
End If
Case vbKeyD
If (direccion2 And derecha) = 0 Then
direccion2 = direccion2 + derecha
End If
Case vbKeyW
If (direccion2 And arriba) = 0 Then
direccion2 = direccion2 + arriba
End If
Case vbKeyS
If (direccion2 And abajo) = 0 Then
direccion2 = direccion2 + abajo
End If
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
direccion1 = direccion1 - izquierda
Case vbKeyRight
direccion1 = direccion1 - derecha
Case vbKeyUp
direccion1 = direccion1 - arriba
Case vbKeyDown
direccion1 = direccion1 - abajo
Case vbKeyA
direccion2 = direccion2 - izquierda
Case vbKeyD
direccion2 = direccion2 - derecha
Case vbKeyW
direccion2 = direccion2 - arriba
Case vbKeyS
direccion2 = direccion2 - abajo
End Select
End Sub
Private Sub Timer1_Timer()
If direccion1 And izquierda Then
Shape1.Left = Shape1.Left - 5
End If
If direccion1 And derecha Then
Shape1.Left = Shape1.Left + 5
End If
If direccion1 And arriba Then
Shape1.Top = Shape1.Top - 5
End If
If direccion1 And abajo Then
Shape1.Top = Shape1.Top + 5
End If
If direccion2 And izquierda Then
Shape2.Left = Shape2.Left - 5
End If
If direccion2 And derecha Then
Shape2.Left = Shape2.Left + 5
End If
If direccion2 And arriba Then
Shape2.Top = Shape2.Top - 5
End If
If direccion2 And abajo Then
Shape2.Top = Shape2.Top + 5
End If
End Sub
Shape1 se mueve con las flechas y Shape2 con W, A, S, D
formas te pongu un ejemplo, abre un formulario e
inserta 2 controles shape y un timer con Interval=50,
además al form le pones KeyPreview=True y ScaleMode=3-Pixels.
Después pon este código:
Private Const izquierda As Byte = 1
Private Const derecha As Byte = 2
Private Const arriba As Byte = 4
Private Const abajo As Byte = 8
Dim direccion1 As Byte, direccion2 As Byte
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
If (direccion1 And izquierda) = 0 Then
direccion1 = direccion1 + izquierda
End If
Case vbKeyRight
If (direccion1 And derecha) = 0 Then
direccion1 = direccion1 + derecha
End If
Case vbKeyUp
If (direccion1 And arriba) = 0 Then
direccion1 = direccion1 + arriba
End If
Case vbKeyDown
If (direccion1 And abajo) = 0 Then
direccion1 = direccion1 + abajo
End If
Case vbKeyA
If (direccion2 And izquierda) = 0 Then
direccion2 = direccion2 + izquierda
End If
Case vbKeyD
If (direccion2 And derecha) = 0 Then
direccion2 = direccion2 + derecha
End If
Case vbKeyW
If (direccion2 And arriba) = 0 Then
direccion2 = direccion2 + arriba
End If
Case vbKeyS
If (direccion2 And abajo) = 0 Then
direccion2 = direccion2 + abajo
End If
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
direccion1 = direccion1 - izquierda
Case vbKeyRight
direccion1 = direccion1 - derecha
Case vbKeyUp
direccion1 = direccion1 - arriba
Case vbKeyDown
direccion1 = direccion1 - abajo
Case vbKeyA
direccion2 = direccion2 - izquierda
Case vbKeyD
direccion2 = direccion2 - derecha
Case vbKeyW
direccion2 = direccion2 - arriba
Case vbKeyS
direccion2 = direccion2 - abajo
End Select
End Sub
Private Sub Timer1_Timer()
If direccion1 And izquierda Then
Shape1.Left = Shape1.Left - 5
End If
If direccion1 And derecha Then
Shape1.Left = Shape1.Left + 5
End If
If direccion1 And arriba Then
Shape1.Top = Shape1.Top - 5
End If
If direccion1 And abajo Then
Shape1.Top = Shape1.Top + 5
End If
If direccion2 And izquierda Then
Shape2.Left = Shape2.Left - 5
End If
If direccion2 And derecha Then
Shape2.Left = Shape2.Left + 5
End If
If direccion2 And arriba Then
Shape2.Top = Shape2.Top - 5
End If
If direccion2 And abajo Then
Shape2.Top = Shape2.Top + 5
End If
End Sub
Shape1 se mueve con las flechas y Shape2 con W, A, S, D
