Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Reaper~ on January 27, 2005, 04:49 PM

Title: Project keeps freezing with after this code..
Post by: Reaper~ on January 27, 2005, 04:49 PM
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?

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Do
Shape1.Move 550
Loop
End If
End Sub


or I tried this 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


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:


Private Sub Form_KeyPress(KeyAscii as Integer)
Do While KeyAscii = vbKeyReturn
Shape1.Move 550
Loop
End Sub
Title: Re: Project keeps freezing with after this code..
Post by: GoSu_KaOs on January 27, 2005, 05:09 PM
If you want it to move only when you press Enter:


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


Moves Left:
Shape1.Left = Shape1.Left - 550


Moves Right:
Shape1.Left = Shape1.Left +550

If you want to click enter once and it moves automaticly, you gotta use at Timer.
Title: Re: Project keeps freezing with after this code..
Post by: Reaper~ on January 27, 2005, 05:15 PM
Oh ok I see. Thanks!   :D
Title: Re: Project keeps freezing with after this code..
Post by: UserLoser. on January 27, 2005, 05:15 PM
Add a message loop (DoEvents) at the end of both of your loops.


While (something)
    'do stuff
    DoEvents
Wend
Title: Re: Project keeps freezing after this code..
Post by: Reaper~ on January 27, 2005, 05:20 PM
I got another question, if I try replacing vbKeyReturn with vbKeyRight the right button doesn't do anything... is vbKeyRight not the right arrow?
Title: Re: Project keeps freezing with after this code..
Post by: GoSu_KaOs on January 27, 2005, 08:22 PM
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
Title: Re: Project keeps freezing after this code..
Post by: Newby on January 27, 2005, 08:39 PM
Quote from: Reaper~ on January 27, 2005, 05:20 PM
I got another question, if I try replacing vbKeyReturn with vbKeyRight the right button doesn't do anything... is vbKeyRight not the right arrow?
You need to use Option Explicit, you'd find out that doesn't exist!
Title: Re: Project keeps freezing with after this code..
Post by: Reaper~ on January 27, 2005, 09:56 PM
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

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?
Title: Re: Project keeps freezing with after this code..
Post by: Newby on January 27, 2005, 10:05 PM
Eww. Nevermind.

I thought vbKeyRight didn't exist at all.

My bad, ignore what I said.

But use Option Explicit wherever possible!
Title: Re: Project keeps freezing with after this code..
Post by: Reaper~ on January 27, 2005, 10:11 PM
Oh Ok, but do you have an idea of how to get the right key to work?
Title: Re: Project keeps freezing with after this code..
Post by: Newby on January 27, 2005, 10:56 PM
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.
Title: Re: Project keeps freezing with after this code..
Post by: Reaper~ on January 27, 2005, 11:11 PM
I went to
http://www.gh-gold.co.uk/keycodes.php
and it says the value is 0x25
Is that what you were talking about? If so, it doesn't work it still
Title: Re: Project keeps freezing with after this code..
Post by: Newby on January 27, 2005, 11:22 PM
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.
Title: Re: Project keeps freezing with after this code..
Post by: Reaper~ on January 27, 2005, 11:34 PM
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.
Title: Re: Project keeps freezing with after this code..
Post by: Dyndrilliac on January 28, 2005, 12:55 PM
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.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
Title: Re: Project keeps freezing after this code..
Post by: Reaper~ on January 28, 2005, 08:11 PM
Awesome, thanks!  ;D