Marshall or other. VB question

Want to just shoot the breeze? Forum 42 is the place!

Moderator: Moderators

Post Reply
Kurt_
Portablizer
Posts: 5748
Joined: Thu Nov 24, 2005 10:32 am
Steam ID: kurbert
Location: Ontario, Canada
Contact:

Marshall or other. VB question

Post by Kurt_ »

First off I probaly won't reply for a bit seeing as I have no internet at home.

Ok so this is what I use to move an object smoothly (quick example):

Code: Select all

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    
    Case Is = 38
        tmrup.Enabled = True
    End Select
    
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    
    Case Is = 38
        tmrup.Enabled = False
    End Select
    
End Sub
And it goes to a timer or whatever where the object is moved.

Code: Select all

Private Sub TmrUp_Timer()
    player1.Left = player1.Left + 30
End Sub
This is what I do to move and object smoothly on a keypress.

Usually in visual basic if I did something, there's an easier way to do it.

Is there possibly just a property that needs to be changed to move the object smoothly vs what happens when you press a key (It presses it once, then pauses before gonig into "autofire")

Sorry if this isn't very clear, I didn't spend too much time typing this up.
Hey, sup?
loup
Posts: 303
Joined: Mon May 23, 2005 4:13 pm

Post by loup »

keeping in mind that it's been a long time since I last really worked with VB, I believe the best method is the timer method you are already using. I seem to remember there being a problem with it though, something that I was trying to do once and it didn't work right using the timer method, so I came up with something else, but I really don't remember what the circumstances were or what I did to resolve it anymore.
From the OLD ozforces forums
- Offenders will be bashed. Repeat Offenders will be repeatedly bashed.
marshallh
Moderator
Posts: 2987
Joined: Sat Sep 10, 2005 2:17 pm
360 GamerTag: marshallh
Location: here and there
Contact:

Post by marshallh »

You should avoid timers like the plague, they are inaccurate, a pain to code for, and are poorly suited for games.

No, what you need to do is make a main loop and move everything from there.

Here's what I mean:

Code: Select all

Dim Keys(255) As Boolean
Dim PersonX As Integer
Dim PersonY As Integer

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Keys(KeyCode) = True
    If KeyCode = vbKeyEscape Then Unload Me: End
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Keys(KeyCode) = False
End Sub

Private Sub Form_Load()
    Delay = 20
    Me.Show

    Do
        DelayGame
        DoEvents

        If Keys(vbKeyUp) Then PersonY = PersonY - 10
        If Keys(vbKeyDown) Then PersonY = PersonY + 10
        If Keys(vbKeyLeft) Then PersonX = PersonX - 10
        If Keys(vbKeyRight) Then PersonX = PersonX + 10

        Shape1.Left = PersonX
        Shape1.Top = PersonY


    Loop
End Sub
And in a seperate modTimer.bas file,

Code: Select all

Public NowTime As Long, Delay As Integer
Public StartTick As Long, LastTick As Long

Public Declare Function timeGetTime Lib "winmm.dll" () As Long

Public Sub DelayGame()
    StartTick = timeGetTime()
    NowTime = timeGetTime()
    Do Until NowTime - LastTick > Delay
        DoEvents
        NowTime = timeGetTime()
    Loop
    LastTick = NowTime
End Sub
This really is the best way to go, it's the one I've settled on over 6 years of VB programming.
If you're using DirectX, there are better ways to do this, but I don't tihnk you're quite there yet.
Image
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

Herm? :?

Are you trying to make a repeated fire but with gaps in between?

This may help:

Code: Select all

Module Hello
   Sub Main()
      MsgBox("Hello World!")
   End Sub
End Module
Kurt_
Portablizer
Posts: 5748
Joined: Thu Nov 24, 2005 10:32 am
Steam ID: kurbert
Location: Ontario, Canada
Contact:

Post by Kurt_ »

Very funny skyone.

Yeah marshall, you're a bit above me. I don't really know/care that much about super advanced programming. I'm into doing very simple programs for mild amusement. I just wanted to avoid using to many timers. They get a little screwey with too many.

Can you explain these lines of code? (And does Me indicate the current object/Sub?)

Code: Select all

If KeyCode = vbKeyEscape Then Unload Me: End

Delay = 20 
    Me.Show 

DelayGame 
        DoEvents 
Hey, sup?
vb_master
Moderator
Posts: 4793
Joined: Tue Jun 08, 2004 9:52 pm

Post by vb_master »

If I actually rememberd visual basic (I love that stuff), I could probably figure this out, but since I don't remember it, I can't help you (so much for vb_master anymore :P ).
marshallh
Moderator
Posts: 2987
Joined: Sat Sep 10, 2005 2:17 pm
360 GamerTag: marshallh
Location: here and there
Contact:

Post by marshallh »

Code: Select all

If KeyCode = vbKeyEscape Then Unload Me: End
This just quits the program when you hit the Escape key. Unload Me is a command to unload the form itself from memory, thus ending the program.

Code: Select all

Delay = 20 
This sets the delay between frames to 20 ms. It's used in the subprocedure below. (DelayGame)

Code: Select all

Me.Show
This shows the form manually. Since we are placing the game loop in the Form_Load procedure, it normally won't draw the form until the procedure is stopped. But since the game loop is there, you have to force the window to show up.

Code: Select all

DelayGame 
Just pauses for a little bit, 20ms as explained above. It keeps things from going too fast. This way, the game runs the same speed regardless of CPU speed.

Code: Select all

DoEvents 
This just takes care of Windows junk - stuff like window redrawing, message handling etc. If you forget this the program might freeze, becuase you need this command to tell your program to stop and listen to what Windows has to say to it.
Image
timmeh87
Senior Member
Posts: 3047
Joined: Mon Nov 14, 2005 10:19 pm
Location: Ontario, Canada

Post by timmeh87 »

i believe you need to either call delay game

Code: Select all

call delaygame
or declare delaygame as a function

Code: Select all

Public function DelayGame()
i think thats how it works... i prefer delcaring everything as functions myself.
Image

"Linux is only free if your time is worthless"
marshallh
Moderator
Posts: 2987
Joined: Sat Sep 10, 2005 2:17 pm
360 GamerTag: marshallh
Location: here and there
Contact:

Post by marshallh »

Nope, you only use functions when the code needs to return a value. Subs don't return anything.

This code works, by the way :wink:
I've only used it for 6 years :)
Image
timmeh87
Senior Member
Posts: 3047
Joined: Mon Nov 14, 2005 10:19 pm
Location: Ontario, Canada

Post by timmeh87 »

for some reason i always thought you needed to put "Call" before the name if its a sub.. *shrug*

it dosent HAVE to return a value if its a function. just like you dont HAVE to pass values to subs. as far as i know theres no speciific reason not to use one or the other.
Image

"Linux is only free if your time is worthless"
Post Reply