运行一次随机事件



下面的代码运行并一遍又一遍地重复。我只粘贴了up_timer但其他人也这样做。关于如何让它运行一次,然后重复到随机循环等等的任何想法?

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Application.DoEvents()
    Randomize()
    Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
    If value = 1 Then
        MsgBox("Up")
        up.Start()
    ElseIf value = 2 Then
        MsgBox("Down")
        down.Start()
    ElseIf value = 3 Then
        MsgBox("Left")
        left.Start()
    ElseIf value = 4 Then
        MsgBox("Right")
        right.Start()
    End If
    Timer1.Stop()
End Sub
Private Sub up_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up.Tick
    Static moveCount As Integer = 1
        If Me.mob2.Location.Y > 12 Then
            Me.mob2.Location = New Point(Me.mob2.Location.X, Me.mob2.Location.Y - 5)
    End If
    moveCount += 1
    If moveCount = 10 Then
        moveCount = 1
        Me.Timer1.Start()
        Me.up.Stop()
    End If
End Sub

看起来这就是您所需要的,基于您对 timer1 重复相同数字的抱怨

Dim rand1 As New Random(CInt(Date.Now.Ticks And &h0000FFFF))
Dim value As Integer = rand1.Next(1, 4)

这不会重复

另外,您必须Timer1.Stop()移动到方法的开头

最新更新