Results 1 to 14 of 14

Thread: [RESOLVED] keydown trouble

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Resolved [RESOLVED] keydown trouble

    i am making a game where you use the left and right arrow keys to move a little guy left and right on the screen and i have used this code:

    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.  
    3. If KeyCode = vbKeyLeft Then
    4.  x = x - 100
    5.  
    6. Elseif keycode = vbkeyright then
    7.  x = x + 100
    8. end if
    9.  
    10. End Sub

    that moves him left and right but the only thing is that when i hold down the key, he moves forwards for a fraction of a second, stops then starts moving normally until i let go. does anyone know how to just make it start moving normally as soon as i press the key?
    also if i have any text boxes or buttons on my form then this code doesn't work, how do i get it to work while it has buttons and text boxes on it?

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: keydown trouble

    Set a flag that will not let the keydown code run until the keyup event clear the flag.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: keydown trouble

    sorry i don't know what you mean by a flag

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: keydown trouble

    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.  
    3.    [B]if KeyFlag = True then exit sub[/B]
    4.  
    5.    If KeyCode = vbKeyLeft Then
    6.        x = x - 100
    7.  
    8.    Elseif keycode = vbkeyright then
    9.        x = x + 100
    10.    End if
    11.  
    12.    [B]KeyFlag = True[/B]
    13.  
    14. End Sub
    15.  
    16. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    17.    KeyFlag = False
    18. End Sub

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: keydown trouble

    no it still does the motion that i described in my first post

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: keydown trouble

    The the problem is with the rest of your code that moves him. Which you doid not show.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: keydown trouble

    here it is



    VB Code:
    1. Dim x As Integer
    2.  
    3.  
    4. Private Sub Form_Load()
    5. x = 2000
    6. End Sub
    7.  
    8. Private Sub Timer1_Timer()
    9.  Line3.X1 = x
    10.  Line3.X2 = x
    11.  Line4.X1 = x - 450
    12.  Line4.X2 = x
    13.  Line5.X1 = x + 450
    14.  Line5.X2 = x
    15. Line2.X1 = x
    16. Line2.X2 = x + 240
    17. Line1.X1 = x
    18. Line1.X2 = x - 240
    19. Shape2.Left = x - 240
    20. Shape3.Left = x - 135
    21. Shape4.Left = x + 45
    22. Shape5.Left = x - 120
    23.  
    24. End Sub

    the timer1.interval is set to 1

  8. #8
    Lively Member ThE_EnGinE's Avatar
    Join Date
    Jun 2006
    Location
    Liverpool, UK
    Posts
    87

    Re: keydown trouble

    I don't know whether this is just my stupidity or not but i'm finding it really hard to decipher exactly what everything does and is for. If you give me your whole code and the amount of lines, shapes etc then I may be able to re-create your code and see if I can fix it.
    Slow Cheetah come before my forest!
    Looks like it's on today.
    Slow Cheetah come, it's so euphoric!
    No matter what they say.



    If you have the answer to your question. Click "Thread Tools" and then "Mark Thread Resolved".

    If you find a post useful. Rate it by clicking "Rate This Post" on the left of the post.

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: keydown trouble

    You possibly need to erase that which is on the screen (the guy) before moving in the new direction. Also try disabling the timer when you press the down key and reenabling it upon exit.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: keydown trouble

    here is my project m
    Attached Files Attached Files

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: keydown trouble

    I think what killo wants is an automatic repeat of his KeyDown code for as long as the key is held down.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  12. #12
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: keydown trouble

    Don't update X every time the keydown event fires, update x using an increment value when the Timer event fires (ie when it is time to move). Set the increment value only when the key first goes down and reset it when the key goes up.

    This has the added benefit of being able to use multiple keys. If both the Left and Right keys are down, the character should basically stop. As it is now the character will move based on the last key to be pressed. When you add the ability to Move Up or Down, pressing (for example) Left and Up will cause the character to move diagonally.

    Here check out this code.

    VB Code:
    1. Dim x As Integer
    2. Dim increment As Integer
    3. Dim blnLeftDown As Boolean
    4. Dim blnRightDown As Boolean
    5.  
    6. Private Sub Form_Load()
    7. x = 2000
    8. End Sub
    9. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    10.  
    11.    If KeyCode = vbKeyLeft And blnLeftDown = False Then
    12.        increment = increment - 100
    13.        blnLeftDown = True
    14.    ElseIf KeyCode = vbKeyRight And blnRightDown = False Then
    15.        increment = increment + 100
    16.        blnRightDown = True
    17.    End If
    18. End Sub
    19.  
    20. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    21.    If KeyCode = vbKeyLeft Then
    22.        increment = increment + 100
    23.        blnLeftDown = False
    24.  
    25.    ElseIf KeyCode = vbKeyRight Then
    26.        increment = increment - 100
    27.         blnRightDown = False
    28.  
    29.    End If
    30. End Sub
    31.  
    32.  
    33. Private Sub Timer1_Timer()
    34. [B] x = x + increment[/B]
    35.  Line3.X1 = x
    36.  Line3.X2 = x
    37.  Line4.X1 = x - 450
    38.  Line4.X2 = x
    39.  Line5.X1 = x + 450
    40.  Line5.X2 = x
    41. Line2.X1 = x
    42. Line2.X2 = x + 240
    43. Line1.X1 = x
    44. Line1.X2 = x - 240
    45. Shape2.Left = x - 240
    46. Shape3.Left = x - 135
    47. Shape4.Left = x + 45
    48. Shape5.Left = x - 120
    49.  
    50. End Sub

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: keydown trouble

    thanks that works perfectly

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: [RESOLVED] keydown trouble

    if i put a textbox on my form then this code doesnt work because the left and right keys are used for moving the cursor in the text box rather than
    moving the guy. it also happens if i use command buttons. how do i get it to work with commnd buttons and text boxes?

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