Valhalla Legends Forums Archive | Visual Basic Programming | Project keeps freezing with after this code..

AuthorMessageTime
Reaper
I'm trying to get it so that when the user presses the over button (in this case the button is Enter, or Return) a shape will move over and everytime you press it, the shape moves, then the program freezes up. Can someone explain this to me and help me out?
[code]
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Do
Shape1.Move 550
Loop
End If
End Sub
[/code]

or I tried this code:

[code]
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim i As Long
If KeyAscii = vbKeyReturn Then
For i = 1 To 99999
Shape1.Move 550
Next
End If
End Sub
[/code]

And it just moves once and then you can press it over and over and it will do nothing.. but it doesn't freeze with this one...

Another freezing one is:

[code]
Private Sub Form_KeyPress(KeyAscii as Integer)
Do While KeyAscii = vbKeyReturn
Shape1.Move 550
Loop
End Sub
[/code]
January 27, 2005, 10:49 PM
GoSu_KaOs
If you want it to move only when you press Enter:

[code]
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Shape1.Left = Shape1.Left - 550
End If
End Sub
[/code]

Moves Left:
[code]Shape1.Left = Shape1.Left - 550[/code]


Moves Right:
[code]Shape1.Left = Shape1.Left +550[/code]

If you want to click enter once and it moves automaticly, you gotta use at Timer.
January 27, 2005, 11:09 PM
Reaper
Oh ok I see. Thanks!  :D
January 27, 2005, 11:15 PM
UserLoser.
Add a message loop (DoEvents) at the end of both of your loops.

[code]
While (something)
    'do stuff
    DoEvents
Wend
[/code]
January 27, 2005, 11:15 PM
Reaper
I got another question, if I try replacing vbKeyReturn with vbKeyRight the right button doesn't do anything... is vbKeyRight not the right arrow?
January 27, 2005, 11:20 PM
GoSu_KaOs
Not sure about this. Try askin on http://www.visualbasicforum.com/

EDIT: Forget that, Open Vb, pres F2. A window opens, type in keycode in the search feild and press the search button. You should see all the vbKey..... codes there.

BTW:

Up: vbKeyUp
Down: vbKeyDown
Left: vbKeyLButton
Right: vbKeyRButton
January 28, 2005, 2:22 AM
Newby
[quote author=Reaper~ link=topic=10336.msg97001#msg97001 date=1106868052]
I got another question, if I try replacing vbKeyReturn with vbKeyRight the right button doesn't do anything... is vbKeyRight not the right arrow?
[/quote]
You need to use Option Explicit, you'd find out that doesn't exist!
January 28, 2005, 2:39 AM
Reaper
Yeah you can see all the keycodes by right-clicking in the coding and going to "List Properties/Methods" and as for:

[quote]
Up: vbKeyUp
Down: vbKeyDown
Left: vbKeyLButton
Right: vbKeyRButton
[/quote]

That still doesn't work and using Option Explicit doesn't do anything, no errors come up and it's still under the list I don't get what you mean Newby?
January 28, 2005, 3:56 AM
Newby
Eww. Nevermind.

I thought vbKeyRight didn't exist at all.

My bad, ignore what I said.

But use Option Explicit wherever possible!
January 28, 2005, 4:05 AM
Reaper
Oh Ok, but do you have an idea of how to get the right key to work?
January 28, 2005, 4:11 AM
Newby
A way to figure out which key it is. (psuedo)

Breakpoint "OnKeyDown" or something similar (Debug.Print on key down)

Push the right key, check which key number was passed in, and stick that where you are using vbKeyRight or whatever.
January 28, 2005, 4:56 AM
Reaper
I went to
[url]http://www.gh-gold.co.uk/keycodes.php[/url]
and it says the value is 0x25
Is that what you were talking about? If so, it doesn't work it still
January 28, 2005, 5:11 AM
Newby
It's vbKeyRight.

I don't know why it doesn't work. I'm busy at the moment, I'll try and figure it out after homework.
January 28, 2005, 5:22 AM
Reaper
Lol (0x27)  :-[ I'm sorry I just looked too fast I guess, but still doesn't solve the problem, whenever you get the chance if you'd take a look it'd be much appreciated.
January 28, 2005, 5:34 AM
Dyndrilliac
This works 100%. Make a Form and a Shape control. Name the Shape control "Shape" and the Form "frmMain". Then copy/paste the following code into the Form's code.[code]Option Explicit

Dim Paused As Boolean

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case (KeyCode)
        Case vbKeyRight
            If Paused = True Then
                Exit Sub
            End If
            Shape.Left = Shape.Left + 20
            frmMain.Refresh
        Case vbKeyLeft
            If Paused = True Then
                Exit Sub
            End If
            Shape.Left = Shape.Left - 20
            frmMain.Refresh
        Case vbKeyUp
            If Paused = True Then
                Exit Sub
            End If
            Shape.Top = Shape.Top - 20
            frmMain.Refresh
        Case vbKeyDown
            If Paused = True Then
                Exit Sub
            End If
            Shape.Top = Shape.Top + 20
            frmMain.Refresh
        Case vbKeyPause
            frmMain.Cls
            If Paused = True Then
                Print "Game Is Not Paused."
                Paused = False
            Else
                Print "Game Is Paused."
                Paused = True
            End If
    End Select
End Sub

Private Sub Form_Load()
    Paused = False
    frmMain.Caption = "Shape Game"
    frmMain.BackColor = vbBlack
    frmMain.ScaleMode = 3
    frmMain.Width = 8895
    frmMain.ScaleWidth = 587
    frmMain.ScaleHeight = 292
    frmMain.Height = 4920
    frmMain.ForeColor = vbRed
    frmMain.FontSize = 12
    frmMain.Font = "MS Sans Serif"
    Shape.BackColor = vbGreen
    Shape.BorderColor = vbYellow
    Shape.BackStyle = 1
    Shape.BorderStyle = 3
    Shape.Height = 33
    Shape.Width = 33
    Shape.Shape = 1
    Shape.Left = 272
    Shape.Top = 112
End Sub[/code]
January 28, 2005, 6:55 PM
Reaper
Awesome, thanks!  ;D
January 29, 2005, 2:11 AM

Search