运行一次Random事件?

时间:2014-01-07 18:13:19

标签: vb.net

下面的代码会一直运行并不断重复。我只粘贴了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

1 个答案:

答案 0 :(得分:1)

根据您对timer1重复相同数字

的抱怨,看起来这就是您所需要的
Dim rand1 As New Random(CInt(Date.Now.Ticks And &h0000FFFF))
Dim value As Integer = rand1.Next(1, 4)

这不会重复

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

相关问题