Visual Basic.NET - Problema con evento KeyDown.

 
Vista:
Imágen de perfil de Evan Hailey
Val: 55
Ha aumentado su posición en 3 puestos en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Problema con evento KeyDown.

Publicado por Evan Hailey (51 intervenciones) el 19/08/2019 21:48:53
Saludos, les comento que estoy con ganas de hacer un juego simple con gdi+ en mis ratos libres y me dispuse a armar los controles, todo iba sobre ruedas hasta que me encontré con el problema de que si desplazo mi personaje por la pantalla de lado a lado va todo mas que bien. El problema viene al intentar simular un salto usando dos rutinas una para ascender y otra para descender que funcionan muy bien pero uso la barra de espacio para saltar y las flechas derecha e izquierda para moverme.

me muevo a los lados y si salto el evento keydown cancela la tecla sostenida y da lugar a la nueva pulsada, eso lo comprendo pero no se como emular una solución, si alguien me puede tirar alguna idea me seria de gran ayuda!

Dejo lo que tengo por ahora...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Public Class Form1
 
    Dim jumpLimit As Short = 100
    Dim charGround As Short = 200
 
    Private charSize As New Size(30, 30)
    Private charPos As New Point(200, charGround)
    Private character As Rectangle
 
    Private WithEvents tmrRefresh As New Timer
    Private GameRefresh As Short = 60   ' ms.
 
    Private counterTimer As Short = 0
 
    Private _jumpAction As Byte = 0
    Public Property JumpAction() As Byte
        Get
            Return _jumpAction
        End Get
        Set(ByVal value As Byte)
            _jumpAction = value
            Select Case value
                Case Is = 0     ' De pie.
                    ' *****
                Case Is = 1     ' Ascendiendo.                   
                    Call JumpUp()
                Case Is = 2     ' Descendiendo.
                    Call JumpDown()
            End Select
        End Set
    End Property
 
    Public Sub New()
        ' Llamada necesaria para el diseñador.
        InitializeComponent()
        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.DoubleBuffered = True
        tmrRefresh.Interval = GameRefresh
        tmrRefresh.Start()
    End Sub
 
    Private Sub Form1_KeyDown(sender As Object,
                              e As KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Is = Keys.Left
                charPos.X -= 2
            Case Is = Keys.Right
                charPos.X += 2
            Case Is = Keys.Space
                JumpAction = 1
        End Select
    End Sub
 
    Private Sub JumpUp()
        If charPos.Y = jumpLimit Then JumpAction = 2 : Exit Sub
        If charPos.Y > jumpLimit And JumpAction = 1 Then
            charPos.Y -= 5
        End If
    End Sub
 
    Private Sub JumpDown()
        If charPos.Y = charGround Then JumpAction = 0 : Exit Sub
        charPos.Y += 5
    End Sub
 
    Private Sub tmrRefresh_Tick(sender As Object,
                                e As EventArgs) Handles tmrRefresh.Tick
        ' Contador de tiempo.
        If counterTimer < 99 Then
            counterTimer += 1 : Else : counterTimer = 0
        End If
        If JumpAction = 1 Then JumpUp()
        If JumpAction = 2 Then JumpDown()
        Me.Refresh()
    End Sub
 
    Private Sub draw(e As PaintEventArgs)
        character = New Rectangle(charPos, charSize)
        e.Graphics.FillRectangle(Brushes.Red, character)
    End Sub
 
    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        Call draw(e)
    End Sub
 
End Class
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder