|
Post by Nmixer on May 18, 2007 21:07:56 GMT
wondering how to program 8 directional movement with vb any ideas?
|
|
|
Post by Al Young on May 18, 2007 21:12:10 GMT
How do you mean? 8 directional as in N, NE, E, SE, S, SW, W, NW?
|
|
|
Post by Nmixer on May 19, 2007 0:27:35 GMT
How do you mean? 8 directional as in N, NE, E, SE, S, SW, W, NW? Thats exactly what i mean... Because when using 'keypress' it only allows one instance of a key to be pushed at one time...
|
|
|
Post by Al Young on May 19, 2007 9:13:44 GMT
Private Class KeyCombination Public Left, Right, Down, Up As Boolean
Public Overloads Function Equals( _ ByVal obj As KeyCombination) As Boolean
Return Me.Left = obj.Left AndAlso Me.Right = obj.Right _ AndAlso Me.Down = obj.Down AndAlso Me.Up = obj.Up End Function Public Sub New() 'empty End Sub Public Sub New(ByVal Left As Boolean, ByVal Right As Boolean, _ ByVal Down As Boolean, ByVal Up As Boolean)
Me.Left = Left Me.Right = Right Me.Up = Up Me.Down = Down End Sub End Class
Private Class KeyCombinations Public Shared ReadOnly None As New KeyCombination( _ False, False, False, False) Public Shared ReadOnly Up As New KeyCombination( _ False, False, False, True) Public Shared ReadOnly Down As New KeyCombination( _ False, False, True, False) Public Shared ReadOnly Left As New KeyCombination( _ True, False, False, False) Public Shared ReadOnly Right As New KeyCombination( _ False, True, False, False) Public Shared ReadOnly LeftUp As New KeyCombination( _ True, False, False, True) Public Shared ReadOnly RightUp As New KeyCombination( _ False, True, False, True) Public Shared ReadOnly LeftDown As New KeyCombination( _ True, False, True, False) Public Shared ReadOnly RightDown As New KeyCombination( _ False, True, True, False) End Class
Private Class KeyCombinationWithOffset Public ReadOnly KeyCombination As KeyCombination Public ReadOnly Offset As Point
Public Sub New(ByVal KeyCombination As KeyCombination, _ ByVal Offset As Point)
Me.KeyCombination = KeyCombination Me.Offset = Offset End Sub Public Sub New(ByVal KeyCombination As KeyCombination, _ ByVal OffsetX As Integer, ByVal OffsetY As Integer) MyClass.New(KeyCombination, New Point(OffsetX, OffsetY)) End Sub End Class
Private f_KeyCombination As New KeyCombination
Private Shared ReadOnly AllCombinations As KeyCombinationWithOffset() _ = {New KeyCombinationWithOffset(KeyCombinations.Left, -1, 0), _ New KeyCombinationWithOffset(KeyCombinations.Right, 1, 0), _ New KeyCombinationWithOffset(KeyCombinations.Up, 0, -1), _ New KeyCombinationWithOffset(KeyCombinations.Down, 0, 1), _ New KeyCombinationWithOffset(KeyCombinations.LeftUp, -1, -1), _ New KeyCombinationWithOffset(KeyCombinations.LeftDown, -1, 1), _ New KeyCombinationWithOffset(KeyCombinations.RightUp, 1, -1), _ New KeyCombinationWithOffset(KeyCombinations.RightDown , 1, 1) _ }
Private Sub Timer1_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Timer1.Tick
For i As Integer = 0 To AllCombinations.Length - 1 Dim combi As KeyCombinationWithOffset = AllCombinations(i) If combi.KeyCombination.Equals(f_KeyCombination) Then Dim p As Point = Me.PictureBox1.Location() p.X += combi.Offset.X p.Y += combi.Offset.Y Me.PictureBox1.Location = p Exit For End If Next
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles MyBase.KeyDown
Dim EnableTimer As Boolean = True
Select Case e.KeyCode Case Keys.Left f_KeyCombination.Left = True Case Keys.Right f_KeyCombination.Right = True Case Keys.Up f_KeyCombination.Up = True Case Keys.Down f_KeyCombination.Down = True Case Else EnableTimer = False End Select
If EnableTimer Then Timer1.Enabled = True
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles MyBase.KeyUp
Select Case e.KeyCode Case Keys.Left f_KeyCombination.Left = False Case Keys.Right f_KeyCombination.Right = False Case Keys.Up f_KeyCombination.Up = False Case Keys.Down f_KeyCombination.Down = False End Select
If f_KeyCombination.Equals(KeyCombinations.None) Then Timer1.Enabled = False End If
End Sub
Something like that, maybe?
|
|
|
Post by Nmixer on May 20, 2007 21:16:05 GMT
i'm wondering what this is for? is this actually for VB6?
i'm going to try and look at the code and see if i can make it work. but so far, i have a lot of errors.
|
|
|
Post by Al Young on May 20, 2007 21:25:19 GMT
Yeah, it is for VB6. It worked for me. You will need to add a timer though.
|
|
|
Post by Nmixer on May 20, 2007 21:29:11 GMT
Oh, this goes inside of a timer? edit:ha ha.... thats me not thinking before i post... all i have to do is modify all the spacing... because right now theres a lot of red so i think it might work ! (or will work since you have it working)
|
|
|
Post by Nmixer on May 20, 2007 21:44:20 GMT
heres an image of how the code looks when pasted inside the vb code editor...
|
|
|
Post by Mucleus on Mar 8, 2008 14:24:31 GMT
That doesn't look like VB6 to me... The way I would do it is to use the GetAsyncKeyState API (shown below), which returns true or false for whether a key is pressed. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer() Caption = "Left: " & CBool(GetAsyncKeyState(vbKeyLeft)) & " " & _ "Right: " & CBool(GetAsyncKeyState(vbKeyRight)) & " " & _ "Up: " & CBool(GetAsyncKeyState(vbKeyUp)) & " " & _ "Down: " & CBool(GetAsyncKeyState(vbKeyDown)) End Sub
This works for two keys simultaneously. Beyond that, some combinations seem to work and others don't (I blame windows ), e.g. Left-Down-Right is detected, Left-Up-Right is not.
|
|
|
Post by Virtuoso on Mar 11, 2008 19:41:13 GMT
That doesn't look like VB6 to me... The way I would do it is to use the GetAsyncKeyState API (shown below), which returns true or false for whether a key is pressed. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer() Caption = "Left: " & CBool(GetAsyncKeyState(vbKeyLeft)) & " " & _ "Right: " & CBool(GetAsyncKeyState(vbKeyRight)) & " " & _ "Up: " & CBool(GetAsyncKeyState(vbKeyUp)) & " " & _ "Down: " & CBool(GetAsyncKeyState(vbKeyDown)) End Sub
This works for two keys simultaneously. Beyond that, some combinations seem to work and others don't (I blame windows ), e.g. Left-Down-Right is detected, Left-Up-Right is not. Thanks for helping, but the topic is nearly a year old. Something tells me the thread creator may not be around anymore. xD
|
|