Results 1 to 24 of 24

Thread: I have a question.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    What code do you have to put in so that if you press the
    Up key, then it will go up and if you press the Down key, it will go down?

  2. #2
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    I'll assume that you want to move the form

    Don't forget to set the KeyPreview property to TRUE.

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            Me.Top = Me.Top - 120
        Case vbKeyDown
            Me.Top = Me.Top + 120
        Case vbKeyLeft
            Me.Left = Me.Left - 120
        Case vbKeyRight
            Me.Left = Me.Left + 120
    End Select
    End Sub
    You move the form with the arrow keys (up, down, left or right). This example moves the form for 120 twips. Hope this helps.

    [Edited by Arcom on 01-12-2001 at 09:02 PM]

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    What will go up/down?
    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Well, sort of, I meant like an object.

    I'm trying to make a side scrolling shooting game so..

  5. #5
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    In that case the code is same as before, just instead of ME keyword put the name of the object you want to move.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Oh, ok.

    When I tried that though, it would only move left once, or right once.

    Would you know how to fix that?
    (I'm pretty new at VB)

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Sorry, I didn't switch all the Me's...

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Oh yeah, i almost forgot.

    You know when i press down, it always goes down then puases and then goes...?

    How can i make it go smoothly?

  9. #9
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    Try this:

    Code:
    Dim blnMove As Boolean ' indicates wheter to move the object or not
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    blnMove = True ' set to move the object
    Do
        Select Case KeyCode
            Case vbKeyUp
                Me.Top = Me.Top - 20
            Case vbKeyDown
                Me.Top = Me.Top + 20
            Case vbKeyLeft
                Me.Left = Me.Left - 20
            Case vbKeyRight
                Me.Left = Me.Left + 20
        End Select
        DoEvents ' let the system handle other things
    Loop While blnMove ' loop while the value of blnMove = TRUE
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    blnMove = False ' set not to move the object
    End Sub
    This code will move the form around the screen (smoothly) until you release any key. To make the object moves more smoothly, try decreasing the numeric values (I put 20 instead of 120 this time).

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Umm..Whenever i release the button, it keeps on going and going and going and going....

    It even goes out of the screen.

  11. #11
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    Hmmm...strange, it works fine for me. Have you put the DOEVENTS before the LOOP UNTIL... statement? If not, there's your problem.

    Try creating a new Standard EXE project and paste the code from here.

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Oh, it said Loop while but whem i put Loop Until, it just goes back to the beginning.

  13. #13
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    You can use the loop statement in two ways:


    1. Loop Until Not blnMove (same as Loop Until blnMove = False)

    2. Loop While blnMove (same as Loop While blnMove = True)

  14. #14

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    What does the Loop do? Is it supposed to continue it?

  15. #15
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    Aha, it is supposed to move the object (in this case - form) until you release the key.

    BTW: Is the code working?

  16. #16

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    err...no.

    I tried fooling around with it but it still didn't work .

  17. #17

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Nevermind, I tried it again and it worked!

    Oh, how can i make it go diagonal?

  18. #18

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Hmmm, just guessing but would it be somthing like this?


    Case vbKeyRight+Down
    Me.Left = Me.Left +20


  19. #19
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    Nope

    You have to use different technique for that.

    Code:
    Dim blnKeys(256) As Boolean ' buffer for key status
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    blnKeys(KeyCode) = True' set that the key is pressed
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    blnKeys(KeyCode) = False' set that the key is released
    End Sub
    
    Public Sub RunMain()
    Do
        ' Check for pressed keys and move form accordingly
        If blnKeys(vbKeyUp) Then Me.Top = Me.Top - 20
        If blnKeys(vbKeyDown) Then Me.Top = Me.Top + 20
        If blnKeys(vbKeyLeft) Then Me.Left = Me.Left - 20
        If blnKeys(vbKeyRight) Then Me.Left = Me.Left + 20
        DoEvents
    Loop Until blnKeys(vbKeyEscape) ' loop until you press ESC
    Unload Me
    End Sub
    
    Private Sub Form_Load()
    Me.Show
    Call RunMain
    End Sub
    Now you can move the form in all directions. When you press the ESC key, the program will exit.

  20. #20

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Wow!

    Thanks!!

  21. #21

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    If I put in a Picture, do I still have to switch Me to (ex.) Picture1?
    I switched them but it won't seem to work...

  22. #22

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14
    Yay! I fixed, I just had to use a Image box instead of a Picture...but it keeps on flickering and how can I make it so it can't go out of the screen?

  23. #23
    Lively Member dlm's Avatar
    Join Date
    Oct 2000
    Location
    Geraardsbergen(Belgium)
    Posts
    91
    Hi,

    You have to place an extra test in it...

    Code:
        If blnKeys(vbKeyUp) Then 
            If Picture1.Top > 0
                Picture1.Top = Picture1.Top - 20
            End If
        End If
        If blnKeys(vbKeyDown) Then 
            If Picture1.Top < Me.Height
                Picture1.Top = Picture1.Top + 20
            End If
        End If
    If you can solve the problem, why worrying about it…
    If you can’t solve the problem, worrying won’t help…

    De la Motte Günther

  24. #24
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    As for the PictureBox, set the KeyPreview property of a Form to True, or you can paint the picture directly on the form if you want, like this:


    Code:
    Dim picThing As New StdPicture ' picture to paint
    Dim blnKeys(256) As Boolean ' buffer for key status
    Dim lX As Long, lY As Long ' picture position
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    blnKeys(KeyCode) = True' set that the key is pressed
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    blnKeys(KeyCode) = False' set that the key is released
    End Sub
    
    Private Sub Form_Load()
    ' Replace this path with a valid one
    Set picThing = LoadPicture(App.Path & "\thing.bmp")
    Me.AutoRedraw = True
    Me.Show
    Call RunMain
    End Sub
    
    Public Sub RunMain()
    Do
        ' Check for pressed keys and move pic (if it's not out of range)
        If blnKeys(vbKeyUp) And (lY <= Me.ScaleHeight) Then lY = lY - 20
        If blnKeys(vbKeyDown) And (lY >= 0) Then lY = lY + 20
        If blnKeys(vbKeyLeft) And (lX <= Me.ScaleWidth) Then lX = lX - 20
        If blnKeys(vbKeyRight) And (lX >= 0) Then lX = lX + 20
        ' Paint it
        Me.Cls
        Me.PaintPicture picThing, lX, lY
        Me.Refresh
        DoEvents
    Loop Until blnKeys(vbKeyEscape) ' loop until you press ESC
    Unload Me
    End Sub

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