1

I made a loop below to make a monster move around in my game, when the loops runs once the whole game freezes... any ideas to make the loop repeat every 5 seconds?

            Randomize()
        Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
    Do
        If value = 1 Then
            If Me.mob2.Location.X < 750 Then
                Me.mob2.Location = New Point(Me.mob2.Location.X + 1, Me.mob2.Location.Y)
            End If
        ElseIf value = 2 Then
            If Me.mob2.Location.Y < 549 Then
                Me.mob2.Location = New Point(Me.mob2.Location.X, Me.mob2.Location.Y + 1)
            End If
        ElseIf value = 3 Then
            If Me.mob2.Location.X > 12 Then
                Me.mob2.Location = New Point(Me.mob2.Location.X - 1, Me.mob2.Location.Y)
            End If
        ElseIf value = 4 Then
            If Me.mob2.Location.X < 750 Then
                Me.mob2.Location = New Point(Me.mob2.Location.X + 1, Me.mob2.Location.Y)
            End If
        End If
    Loop

Timer That doesn't work:

    Private Sub Timer()
    ' Timer.interval = 1000
End Sub
Meatie
  • 51
  • 1
  • 2
  • 8
  • 1
    You can put a timer on that function to be called every 5 seconds, then you can remove the loop entirely. – Kevin DiTraglia Jan 06 '14 at 19:10
  • The Problem is: what timer, I did Thread.Sleep(50) and it still froze... plus that freezes the while program, not the function... – Meatie Jan 06 '14 at 19:13
  • You have to make a timer, it is an object in VB (you can drag it in from the UI editor or declare one yourself on load). The timer will have a function that it will call every x milleseconds, so you just have the timer's tick call your function, then remove the loop all together. – Kevin DiTraglia Jan 06 '14 at 19:15
  • 1
    As you have it now you just have an infinite loop, so it will just keep going forever locking your program. Adding a sleep doesn't change the fact that it is an infinite loop and will never complete thus locking your program. – Kevin DiTraglia Jan 06 '14 at 19:16
  • oh, I was trying to make it like a AI for my monster to walk around, any ideas how to do this without a infinite loop? – Meatie Jan 06 '14 at 19:17
  • Timer dosn't seem to work... Heres the code I tryed: `code` Private Sub Timer() ' Timer.interval = 1000 End Sub `code` – Meatie Jan 06 '14 at 19:41

1 Answers1

1

Not a good solution but this will help you to understand the problem

the problem in that your screen is not refreshing try this:

    Do

    application.doevents() '' so each time it loops it will refresh your screen

    If value = 1 Then
        If Me.mob2.Location.X < 750 Then
            Me.mob2.Location = New Point(Me.mob2.Location.X + 1, Me.mob2.Location.Y)
        End If
    ElseIf value = 2 Then
        If Me.mob2.Location.Y < 549 Then
            Me.mob2.Location = New Point(Me.mob2.Location.X, Me.mob2.Location.Y + 1)
        End If
    ElseIf value = 3 Then
        If Me.mob2.Location.X > 12 Then
            Me.mob2.Location = New Point(Me.mob2.Location.X - 1, Me.mob2.Location.Y)
        End If
    ElseIf value = 4 Then
        If Me.mob2.Location.X < 750 Then
            Me.mob2.Location = New Point(Me.mob2.Location.X + 1, Me.mob2.Location.Y)
        End If
    End If
Loop
bto.rdz
  • 6,636
  • 4
  • 35
  • 52
  • 1
    See [Use of Application.DoEvents()](http://stackoverflow.com/questions/5181777/use-of-application-doevents/5183623#5183623) and [How to use DoEvents() without being “evil”?](http://stackoverflow.com/a/11352575/719186) DoEvents is probably the wrong suggestion here. – LarsTech Jan 06 '14 at 19:51
  • I agree, but making a graphic object just with vb no extra libraries is the first mistake – bto.rdz Jan 06 '14 at 19:52
  • Thanks :D that was very helpful, I would Vote the it as useful, but I don't have 15 rep points :P – Meatie Jan 06 '14 at 19:56
  • Is visual basic bad? what language do you suggest for games? @bto.rdz – Meatie Jan 06 '14 at 19:56
  • vb is perfect, but this is not the best solution since your app will be slow, because each time you call doevents it refreshes all your app, check the articles that @LarsTech mention, you can just check my answer as solution click in the check below up and down votes, it will give you some reputation – bto.rdz Jan 06 '14 at 19:58