|
-
Apr 17th, 2001, 10:13 AM
#1
Thread Starter
Evil Genius
can someone do the explanation as to why the left & right here work, but the up & down don't ???
Code:
Private Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
Private Sub Timer1_Timer()
If GetKeyState(vbKeyLeft) And &H8000 Then Picture1.Left = Picture1.Left - 20
If GetKeyState(vbKeyRight) And &H8000 Then Picture1.Left = Picture1.Left + 20
If GetKeyState(vbKeyUp) And &H8000 Then Picture1.Left = Picture1.Top - 20
If GetKeyState(vbKeyDown) And &H8000 Then Picture1.Left = Picture1.Top + 20
End Sub
Doesn't make sense to me. Thanks
-
Apr 17th, 2001, 10:58 AM
#2
Retired VBF Adm1nistrator
I dunno why your code doesnt work.
But why dont you try this approach, its smoother by far !
Just give it a go and you'll see.
Code:
Option Explicit
Private keyUp As Boolean
Private keyDown As Boolean
Private keyLeft As Boolean
Private keyRight As Boolean
Private LastTick As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft:
keyLeft = True
keyRight = False
Case vbKeyRight:
keyLeft = False
keyRight = True
Case vbKeyUp:
keyUp = True
keyDown = False
Case vbKeyDown:
keyUp = False
keyDown = True
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft:
keyLeft = False
Case vbKeyRight:
keyRight = False
Case vbKeyUp:
keyUp = False
Case vbKeyDown:
keyDown = False
End Select
End Sub
Private Sub RunLoop()
Do
DoEvents
If ((GetTickCount - LastTick) > 50) Then
LastTick = GetTickCount
Select Case True
Case keyUp:
Picture1.Top = Picture1.Top - 20
Case keyDown:
Picture1.Top = Picture1.Top + 20
End Select
Select Case True
Case keyLeft:
Picture1.Left = Picture1.Left - 20
Case keyRight:
Picture1.Left = Picture1.Left + 20
End Select
End If
DoEvents
Loop
End Sub
Private Sub Form_Load()
Me.Show
RunLoop
End Sub
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Apr 17th, 2001, 06:38 PM
#3
transcendental analytic
Code:
Private Sub Timer1_Timer()
If GetKeyState(vbKeyLeft) And &H8000 Then Picture1.Left = Picture1.Left - 20
If GetKeyState(vbKeyRight) And &H8000 Then Picture1.Left = Picture1.Left + 20
If GetKeyState(vbKeyUp) And &H8000 Then Picture1.Left = Picture1.Top - 20
If GetKeyState(vbKeyDown) And &H8000 Then Picture1.Left = Picture1.Top + 20
End Sub
I hope you'll notice the bug
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Apr 18th, 2001, 01:51 AM
#4
Retired VBF Adm1nistrator
Ya know, I never even bothered checking if the code was dodgy when I tried it ...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Apr 18th, 2001, 05:04 AM
#5
transcendental analytic
i didn't even try it alex code fullfills it purpose so i didn't bother rewrite it
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Apr 19th, 2001, 05:41 AM
#6
Retired VBF Adm1nistrator
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|