Zwei keys gleichzeitig drücken (zb UP & RIGHT)

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Mangafreak1995.

    Zwei keys gleichzeitig drücken (zb UP & RIGHT)

    Hallo!

    Arbeite derzeit an einem kleinen 2d jump'n'run (oder Platform) Spielchen! Mein Problem ist Folgendes:
    Wenn ich die UP Taste betätige (also einen Sprung simuliere), kann ich während des Sprungs weder nach rechts noch nach links gehen.

    Quellcode:
    frmMain

    VB.NET-Quellcode

    1. Public Class frmMain
    2. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    3. If e.KeyCode < 40 Then keyarray(e.KeyCode) = True
    4. End Sub
    5. Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    6. If e.KeyCode < 40 Then keyarray(e.KeyCode) = False
    7. End Sub
    8. Private Sub tmrGameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrGameTimer.Tick
    9. If keyarray(37) = True Then
    10. If imgPlayer.Left > 100 Then
    11. imgPlayer.Left = imgPlayer.Left - 10
    12. Else
    13. If imgBackground.Left >= 0 Then
    14. Else
    15. imgBackground.Left = imgBackground.Left + 10
    16. imgBottom.Left = imgBottom.Left + 10
    17. imgBrick.Left = imgBrick.Left + 10
    18. End If
    19. End If
    20. End If
    21. If keyarray(39) = True Then
    22. If imgPlayer.Left < 600 Then
    23. imgPlayer.Left = imgPlayer.Left + 10
    24. Else
    25. If imgBackground.Left <= -4200 Then
    26. Else
    27. imgBackground.Left = imgBackground.Left - 10
    28. imgBottom.Left = imgBottom.Left - 10
    29. imgBrick.Left = imgBrick.Left - 10
    30. End If
    31. End If
    32. End If
    33. If keyarray(38) = True Then
    34. Dim Jump As Integer = 0
    35. Do Until Jump = 10
    36. imgPlayer.Top = imgPlayer.Top - 10
    37. Jump = Jump + 1
    38. System.Threading.Thread.Sleep(5)
    39. Me.Refresh()
    40. Loop
    41. Do Until Jump = 0
    42. imgPlayer.Top = imgPlayer.Top + 10
    43. Jump = Jump - 1
    44. System.Threading.Thread.Sleep(5)
    45. Me.Refresh()
    46. Loop
    47. tmrJump.Enabled = False
    48. End If
    49. End Sub
    50. Private Sub tmrCurrentPos_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCurrentPos.Tick
    51. lblPlayerX.Text = "X:" & imgPlayer.Location.X
    52. lblPlayerY.Text = "Y:" & imgPlayer.Location.Y
    53. End Sub
    54. End Class


    Module1

    VB.NET-Quellcode

    1. Module Module1
    2. Public keyarray(39) As Boolean
    3. End Module


    Danke im Voraus!
    Aydo :D :D :D :D :D
    Hab das ganze jetzt mal mit GetAsyncKeyState ausprobiert, klappt trotzdem nicht.
    Denke das liegt an dem Sprung. Hat einer eine Idee wie ich das evtl. umschreiben könnte, sodass ich immernoch die Sprung Animation habe? Will mich aber während dessen trotzdem bewegen können.^^

    Das ganze sieht jetzt so aus:

    VB.NET-Quellcode

    1. Public Class frmMain
    2. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
    3. Private Sub tmrGameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrGameTimer.Tick
    4. Dim Movement(3) As Boolean
    5. Movement(1) = GetAsyncKeyState(Keys.Left) 'nach links
    6. Movement(2) = GetAsyncKeyState(Keys.Right) 'nach rechts
    7. Movement(3) = GetAsyncKeyState(Keys.Up) 'Sprung
    8. If Movement(1) = True Then 'nach links
    9. If imgPlayer.Left > 50 Then
    10. imgPlayer.Left = imgPlayer.Left - 10
    11. Else
    12. If imgBackground.Left >= 0 Then
    13. Else
    14. imgBackground.Left = imgBackground.Left + 10
    15. imgBottom.Left = imgBottom.Left + 10
    16. imgBrick.Left = imgBrick.Left + 10
    17. End If
    18. End If
    19. End If
    20. If Movement(2) = True Then 'nach rechts
    21. If imgPlayer.Left < 600 Then
    22. imgPlayer.Left = imgPlayer.Left + 10
    23. Else
    24. If imgBackground.Left <= -4200 Then
    25. Else
    26. imgBackground.Left = imgBackground.Left - 10
    27. imgBottom.Left = imgBottom.Left - 10
    28. imgBrick.Left = imgBrick.Left - 10
    29. End If
    30. End If
    31. End If
    32. If Movement(3) = True Then 'Sprung
    33. Dim Jump As Integer = 0
    34. Do Until Jump = 10
    35. imgPlayer.Top = imgPlayer.Top - 10
    36. Jump = Jump + 1
    37. System.Threading.Thread.Sleep(5)
    38. Me.Refresh()
    39. Loop
    40. Do Until Jump = 0
    41. imgPlayer.Top = imgPlayer.Top + 10
    42. Jump = Jump - 1
    43. System.Threading.Thread.Sleep(5)
    44. Me.Refresh()
    45. Loop
    46. tmrJump.Enabled = False
    47. End If
    48. End Sub
    49. Private Sub tmrCurrentPos_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCurrentPos.Tick
    50. lblPlayerX.Text = "X:" & imgPlayer.Location.X
    51. lblPlayerY.Text = "Y:" & imgPlayer.Location.Y
    52. End Sub
    53. End Class

    Danke.
    hab sowas auch in Delphi gemacht
    frag einfach ab aber verteil das auf mehrere Variablen

    VB.NET-Quellcode

    1. If e.KeyCode = vb Up Then Jump = True
    2. If e.KeyCode = vbLeft Or e.Keycode = vbRight Then RunDirection (as String/Integer) = e.KeyCode