Results 1 to 6 of 6

Thread: and another NEWBIE one ...

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    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

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Ya know, I never even bothered checking if the code was dodgy when I tried it ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    my approach owns
    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
  •  



Click Here to Expand Forum to Full Width