PDA

Click to See Complete Forum and Search --> : I have a question.


mickey987
Jan 12th, 2001, 07:01 PM
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?

Arcom
Jan 12th, 2001, 07:58 PM
I'll assume that you want to move the form :)

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

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]

HarryW
Jan 12th, 2001, 07:59 PM
What will go up/down?

mickey987
Jan 12th, 2001, 08:09 PM
Well, sort of, I meant like an object.

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

Arcom
Jan 12th, 2001, 08:21 PM
In that case the code is same as before, just instead of ME keyword put the name of the object you want to move.

mickey987
Jan 12th, 2001, 08:29 PM
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)

mickey987
Jan 12th, 2001, 08:30 PM
Sorry, I didn't switch all the Me's...

mickey987
Jan 12th, 2001, 08:55 PM
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?

Arcom
Jan 12th, 2001, 09:12 PM
Try this:

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).

mickey987
Jan 12th, 2001, 09:29 PM
Umm..Whenever i release the button, it keeps on going and going and going and going....

It even goes out of the screen.

Arcom
Jan 12th, 2001, 09:45 PM
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.

mickey987
Jan 12th, 2001, 09:53 PM
Oh, it said Loop while but whem i put Loop Until, it just goes back to the beginning.

Arcom
Jan 12th, 2001, 10:04 PM
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)

mickey987
Jan 12th, 2001, 10:15 PM
What does the Loop do? Is it supposed to continue it?

Arcom
Jan 13th, 2001, 06:55 PM
Aha, it is supposed to move the object (in this case - form) until you release the key.

BTW: Is the code working?

mickey987
Jan 13th, 2001, 10:39 PM
err...no.

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

mickey987
Jan 13th, 2001, 11:23 PM
Nevermind, I tried it again and it worked! :)

Oh, how can i make it go diagonal?

mickey987
Jan 13th, 2001, 11:27 PM
Hmmm, just guessing but would it be somthing like this?


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

Arcom
Jan 14th, 2001, 05:57 AM
Nope :)

You have to use different technique for that.

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.

mickey987
Jan 14th, 2001, 11:27 AM
Wow!

Thanks!! :)

mickey987
Jan 14th, 2001, 11:36 AM
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...:(

mickey987
Jan 14th, 2001, 11:55 AM
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?

dlm
Jan 15th, 2001, 04:31 AM
Hi,

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


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

Arcom
Jan 15th, 2001, 03:39 PM
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:


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