步驟 6:加入計時器
接下來,您可以將 [計時器] 加入至配對遊戲。計時器會等候指定的毫秒數,然後引發事件,稱為「刻度」(Tick)。這對於定期啟動動作或重複動作非常有用。在這個案例中,您將使用計時器讓玩家選擇兩個圖示,如果圖示不相符,則在短時間之後再次隱藏兩個圖示。
若要加入計時器
在 Windows Form 設計工具的工具箱中,選擇 [計時器] (位於 [元件] 分類),再選擇 ENTER 鍵或按兩下計時器,將計時器控制項加入至表單。計時器的圖示 (稱為 [Timer1]),應該會顯示在表單下的空間中,如以下圖片所示。
Timer
注意事項
如果工具箱是空的,請確定先選取表單設計工具 (而不是選取表單的後置程式碼),再開啟工具箱。
選擇 [Timer1] 圖示以選取計時器。在 [屬性] 視窗中,請從檢視事件切換為檢視屬性。然後,將計時器的 [Interval] 屬性設為 750,但是將 [Enabled] 屬性設為 [False]。[Interval] 屬性會告知計時器在「刻度」(Tick) 之間,或當其觸發其 Tick 事件時應等候多久的時間。值為 750 表示會告知計時器等待四分之三秒 (750 毫秒),再引發其 Tick 事件。您只會在玩家選擇第二個標籤時呼叫 Start() 方法,以啟動計時器。
選擇 [Windows Form 設計工具] 中的計時器控制項圖示,然後選擇 ENTER 鍵或按兩下計時器,以加入空的 Tick 事件處理常式。以下列程式碼取代程式碼,或手動輸入下列程式碼至事件處理常式。
''' <summary> ''' This timer is started when the player clicks ''' two icons that don't match, ''' so it counts three quarters of a second ''' and then turns itself off and hides both icons ''' </summary> ''' <remarks></remarks> Private Sub Timer1_Tick() Handles Timer1.Tick ' Stop the timer Timer1.Stop() ' Hide both icons firstClicked.ForeColor = firstClicked.BackColor secondClicked.ForeColor = secondClicked.BackColor ' Reset firstClicked and secondClicked ' so the next time a label is ' clicked, the program knows it's the first click firstClicked = Nothing secondClicked = Nothing End Sub
/// <summary> /// This timer is started when the player clicks /// two icons that don't match, /// so it counts three quarters of a second /// and then turns itself off and hides both icons /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Tick(object sender, EventArgs e) { // Stop the timer timer1.Stop(); // Hide both icons firstClicked.ForeColor = firstClicked.BackColor; secondClicked.ForeColor = secondClicked.BackColor; // Reset firstClicked and secondClicked // so the next time a label is // clicked, the program knows it's the first click firstClicked = null; secondClicked = null; }
Tick 事件處理常式會執行下列三件事情:首先,它會呼叫 Stop() 方法來確定計時器並未執行。接著,它會使用兩個參考變數 firstClicked 和 secondClicked,確定玩家所選的兩個標籤的圖示再次看不見。最後,它會將 firstClicked 和 secondClicked 參考變數重設為 Visual C# 中的 null 和 Visual Basic 中的 Nothing。這個步驟很重要,因為這就是程式本身重設的方式。現在它不會追蹤任何的 Label 控制項,而且已準備好讓玩家再度選擇標籤。
注意事項
Timer 物件具有可以啟動和停止計時器的 Start() 方法和 Stop() 方法。當您在 [屬性] 視窗中將計時器的 [Enabled] 屬性設為 [True] 時,計時器會在程式開始時立即開始計時。但是,當您將它設為 [False] 時,則要等到呼叫其 Start() 方法時才會開始計時。通常,計時器會不斷地引發其 Tick 事件,並使用 [Interval] 屬性來決定在刻度之間要等待多少毫秒。您可能已注意到在 Tick 事件內呼叫計時器之 Stop() 方法的方式。該方式會使計時器進入「一次性模式」(One Shot Mode),表示在呼叫 Start() 方法時,它會等候指定的間隔、觸發單一 Tick 事件,然後停止。
若要查看作用中的新計時器,請移至程式碼編輯器並將下列程式碼加入至 label_Click() 事件處理常式方法的頂端和底端 (您會將 if 陳述式加入至頂端,而將三個陳述式加入至底端;其餘的方法則維持不變)。
''' <summary> ''' Every label's Click event is handled by this event handler ''' </summary> ''' <param name="sender">The label that was clicked</param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click, Label1.Click ' The timer is only on after two non-matching ' icons have been shown to the player, ' so ignore any clicks if the timer is running If Timer1.Enabled Then Exit Sub Dim clickedLabel = TryCast(sender, Label) If clickedLabel IsNot Nothing Then ' If the clicked label is black, the player clicked ' an icon that's already been revealed -- ' ignore the click If clickedLabel.ForeColor = Color.Black Then Exit Sub ' If firstClicked is Nothing, this is the first icon ' in the pair that the player clicked, ' so set firstClicked to the label that the player ' clicked, change its color to black, and return If firstClicked Is Nothing Then firstClicked = clickedLabel firstClicked.ForeColor = Color.Black Exit Sub End If ' If the player gets this far, the timer isn't ' running and firstClicked isn't Nothing, ' so this must be the second icon the player clicked ' Set its color to black secondClicked = clickedLabel secondClicked.ForeColor = Color.Black ' If the player gets this far, the player ' clicked two different icons, so start the ' timer (which will wait three quarters of ' a second, and then hide the icons) Timer1.Start() End If End Sub
/// <summary> /// Every label's Click event is handled by this event handler /// </summary> /// <param name="sender">The label that was clicked</param> /// <param name="e"></param> private void label_Click(object sender, EventArgs e) { // The timer is only on after two non-matching // icons have been shown to the player, // so ignore any clicks if the timer is running if (timer1.Enabled == true) return; Label clickedLabel = sender as Label; if (clickedLabel != null) { // If the clicked label is black, the player clicked // an icon that's already been revealed -- // ignore the click if (clickedLabel.ForeColor == Color.Black) return; // If firstClicked is null, this is the first icon // in the pair that the player clicked, // so set firstClicked to the label that the player // clicked, change its color to black, and return if (firstClicked == null) { firstClicked = clickedLabel; firstClicked.ForeColor = Color.Black; return; } // If the player gets this far, the timer isn't // running and firstClicked isn't null, // so this must be the second icon the player clicked // Set its color to black secondClicked = clickedLabel; secondClicked.ForeColor = Color.Black; // If the player gets this far, the player // clicked two different icons, so start the // timer (which will wait three quarters of // a second, and then hide the icons) timer1.Start(); } }
位於方法頂端的程式碼會檢查計時器是否藉由勾選 [Enabled] 屬性的值來啟動。若是如此,如果玩家選擇第一個和第二個 Label 控制項而且計時器啟動了,則選擇第三個標籤不會執行任何動作。
位於方法底端的程式碼會設定 secondClicked 參考變數,以便追蹤玩家所選擇的第二個 Label 控制項,然後再將該標籤的圖示色彩設成黑色,使其能被看見。然後,它會以一次性模式啟動計時器,因此會等待 750 毫秒,而後引發單一 Tick 事件。計時器的 Tick 事件處理常式會隱藏兩個圖示,並重設 firstClicked 和 secondClicked 參考變數,以便表單準備讓玩家選擇另一組圖示。
儲存並執行您的程式。選擇圖示,它會成為可見狀態。
請選擇其他圖示。它會短暫出現,然後這兩個圖示會消失。重複此動作許多次。表單現在會追蹤您選擇的第一個和第二個圖示,並使用計時器在圖示消失之前暫停追蹤。
若要繼續或檢視
若要移到下一個教學課程步驟,請參閱步驟 7:讓配對保持可見。
若要回到上一個教學課程步驟,請參閱步驟 5:加入標籤參考。