2016-11-22 44 views
0

我想要發生一個動作,然後等待3秒鐘,然後繼續執行其餘的代碼。VB定時器倒數

我有一個名爲tmrTurnTimer設計師定時器設置爲3000的間隔Tick事件如下

Private Sub tmrTurnTimer_Tick(sender As Object, e As EventArgs) Handles tmrTurnTimer.Tick 

    Dim intTimer As Integer = 3 

    If intTimer > 0 Then 
     lblEnemyDamage.Text = intHeroAtk.ToString() 
     intTimer -= 1 
    Else 
     tmrTurnTimer.Stop() 
     lblEnemyDamage.Text = "" 
    End If 
End Sub 

我所說的記號事件的主人公已經有三週秒鐘的延遲,希望之後攻擊然而,在敵人回擊之前,只要單擊「Attack」,同時全部都會立即執行英雄和敵方標籤中的傷害。

'Call Attack Sub with Hero damage ranges plugged in 
      Attack(intHeroAtk, 5, 31, intEnemyHP, 8, 14) 

      'Display damage done in Enemy damage label 
      lblEnemyDamage.Text = intHeroAtk.ToString() 


      'If the enemy health is above zero 
      If intEnemyHP > 0 Then 

       'Start timer and display damage from hero in enemy damage label 
       tmrTurnTimer.Start() 

       'This is where I would like the program to wait for 
       'three seconds before proceeding 

       'Call Attack Sub with Enemy damage ranges plugged in 
       Attack(intEnemyAtk, 5, 21, intHeroHP, 10, 11) 

       'Display damage done in Hero damage label 
       lblHeroDamage.Text = intEnemyAtk.ToString() 

       DisplayStats(intHeroHP, intHeroMP) 

請原諒我,如果這是一個愚蠢的請求。

+1

需要聲明'昏暗intTimer爲整數= 3'的'tmrTurnTimer_Tick'之外方法,否則每次計時器滴答它將被初始化爲3的值,並且永遠不會下降到0.(這只是一個小的觀察,並不能真正解決你所遇到的問題)。 – Bugs

+1

良好的觀察。我會嘗試並報告回來。 – Airvvic

+0

@ Jinx88909在模塊中放置'Public intTimer As Integer = 3'。沒有運氣。它仍然在同一時間進行。 – Airvvic

回答

0

首先關閉計時器不像你想象的那樣工作。他們不會暫停UI線程,他們會做相反的事情,他們會根據自己的線索開始工作,讓UI繼續其業務。計時器會在用戶界面線程過了一段時間後通知用戶。你需要做的是把你的代碼在tmrTurnTimer_Tick事件方法中經過3秒後發生。如果你想讓傷害文字在1秒後消失,那麼我會在不同的定時器上設置爲1秒。

'Call Attack Sub with Hero damage ranges plugged in 
     Attack(intHeroAtk, 5, 31, intEnemyHP, 8, 14) 

     'Display damage done in Enemy damage label 
     lblEnemyDamage.Text = intHeroAtk.ToString() 


     'If the enemy health is above zero 
     If intEnemyHP > 0 Then 

      'Start timer and display damage from hero in enemy damage label 
      tmrTurnTimer.Start() 
    lblEnemyDamage.Text = intHeroAtk.ToString() 
    tmrEnemyDamageTimer.Start() 

轉碼

Private Sub tmrTurnTimer_Tick(sender As Object, e As EventArgs) Handles tmrTurnTimer.Tick 

    tmrTurnTimer.Stop() 

       'Call Attack Sub with Enemy damage ranges plugged in 
       Attack(intEnemyAtk, 5, 21, intHeroHP, 10, 11) 

       'Display damage done in Hero damage label 
       lblHeroDamage.Text = intEnemyAtk.ToString() 

       DisplayStats(intHeroHP, intHeroMP) 
End Sub 

敵人造成傷害代碼(需要創建另一個定時器)

private Sub tmrEnemyDamageTimer_Tick(sender As Object, e As EventArgs) Handles tmrTurnTimer.Tick 

    tmrEnemyDamageTimer.Stop() 
    lblEnemyDamage.Text = "" 
End Sub