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:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft Then
x = x - 100
Elseif keycode = vbkeyright then
x = x + 100
end if
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?
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.
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.
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
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:
Dim x As Integer
Dim increment As Integer
Dim blnLeftDown As Boolean
Dim blnRightDown As Boolean
Private Sub Form_Load()
x = 2000
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft And blnLeftDown = False Then
increment = increment - 100
blnLeftDown = True
ElseIf KeyCode = vbKeyRight And blnRightDown = False Then
increment = increment + 100
blnRightDown = True
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
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?