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?
Printable View
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?
I'll assume that you want to move the form :)
Don't forget to set the KeyPreview property to TRUE.
You move the form with the arrow keys (up, down, left or right). This example moves the form for 120 twips. Hope this helps.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
[Edited by Arcom on 01-12-2001 at 09:02 PM]
What will go up/down?
Well, sort of, I meant like an object.
I'm trying to make a side scrolling shooting game so..
In that case the code is same as before, just instead of ME keyword put the name of the object you want to move.
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)
Sorry, I didn't switch all the Me's...
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?
Try this:
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).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
Umm..Whenever i release the button, it keeps on going and going and going and going....
It even goes out of the screen.
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.
Oh, it said Loop while but whem i put Loop Until, it just goes back to the beginning.
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)
What does the Loop do? Is it supposed to continue it?
Aha, it is supposed to move the object (in this case - form) until you release the key.
BTW: Is the code working?
err...no.
I tried fooling around with it but it still didn't work :(.
Nevermind, I tried it again and it worked! :)
Oh, how can i make it go diagonal?
Hmmm, just guessing but would it be somthing like this?
Case vbKeyRight+Down
Me.Left = Me.Left +20
Nope :)
You have to use different technique for that.
Now you can move the form in all directions. When you press the ESC key, the program will exit.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
Wow!
Thanks!! :)
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...:(
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?
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
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