重复计时器事件10次然后结束它?

时间:2014-01-07 17:30:52

标签: vb.net

我如何制作一个Timer,当它运行大部分代码运行10次然后运行第10次它运行Timer2.Stop()

下面的代码使怪物向右移动5个像素然后停止,我希望它移动一个像素5次然后运行right.Stop()和Timer1.start()

如果有人可以帮我解决这个问题,那就太棒了:D

 Private Sub right_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles right.Tick
    Timer1.Stop()
    If Me.mob2.Location.X < 750 Then
        Me.mob2.Location = New Point(Me.mob2.Location.X + 5, Me.mob2.Location.Y)
    End If
    right.Stop()
    Timer1.Start()
End Sub

1 个答案:

答案 0 :(得分:1)

试试这个:

Private Sub right_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles right.Tick
    Timer1.Stop()
    Static moveCount as Integer = 1
    If Me.mob2.Location.X < 750 Then
        Me.mob2.Location = New Point(Me.mob2.Location.X + 1, Me.mob2.Location.Y)
    End If
    moveCount += 1
    ' edit this for how many times you want it to move
    If moveCount = 5 Then
        right.Stop()
        moveCount = 1
        Timer1.Start()
    End If     
End Sub