创建定时器倒计时VB.Net?

时间:2013-09-28 15:23:56

标签: vb.net timer countdown

我正在创建一个小游戏,当使用点击按钮时,它“攻击”怪物,导致它失去5马力,使用进度条显示。然后同时怪物也会攻击让玩家失去马力。但问题是这些事件发生在同一时间,我希望事件之间有2秒的间隔。我一直试图让计时器事件从今天早上开始工作,但它不会在这里工作是我的代码

   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)           Handles Timer1.Tick

    If Tick = 0 Then
        Timer1.Stop()
        Timer1.Enabled = False
    Else
        Tick -= 1
    End If

End Sub

这是攻击按钮事件

    Private Sub btnAttack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttack.Click
    PlayerAttacks(mhealth, attack)
    textShiftUp(numlines)
    HPBarsAlter(mhealth, health)
    Tick = 2
    Timer1.Start()

    MonsterAttacks(health, mattack, CritRate)
    HPBarsAlter(mhealth, health)
    MobDeath(mhealth, MobNumber)

    Timer1.Stop()

End Sub

如果您需要更多信息,请告诉我,谢谢:)

1 个答案:

答案 0 :(得分:0)

基本上,将你的怪物攻击移动到计时器

Private Sub btnAttack_Click(...)
    btnAttack.Enabled = False              ' disable more attacks

    PlayerAttacks(mhealth, attack)
    textShiftUp(numlines)
    HPBarsAlter(mhealth, health)

    MonsterTimer.Interval = 2000
    MonsterTimer.Start()
End Sub


Private Sub MonsterTimer_Tick(...
    ' not sure what the old code was doing

     MonsterTimer.Stop           ' stop the attacks

     MonsterAttacks(health, mattack, CritRate)
     HPBarsAlter(mhealth, health)
     MobDeath(mhealth, MobNumber)

     btnAttack.Enabled = True              ' allow more attacks

End Sub

EDIT 添加了2行,以便在等待时切换攻击能力。