共用方式為


步驟 6:加入計時器

接下來,您可以將計時器加入至配對遊戲。

若要加入計時器

  1. 移至 [Windows Form 設計工具] 中的 [工具箱]。 按兩下 [Timer] (在 [元件] 類別中),並將計時器加入至表單,使其圖示出現在表單下方的灰色方塊中,如下列圖片所示。

    Timer

    計時器

  2. 按一下 timer1 圖示以選取計時器。 將 [Interval] 屬性設為 750,但是將 [Enabled] 屬性設為 [False]。 [Interval] 屬性會告訴計時器在滴答之間要等候多久,所以這會告訴計時器在它引發第一個 Tick 事件之前要等待四分之三秒 (750 毫秒)。 您不想在程式啟動時啟動計時器。 而是,使用 Start() 方法,以在玩家按下第二個標籤時啟動計時器。

  3. 請按兩下 [Windows Form 設計工具] 中的 Timer 控制項圖示,以加入 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 事件。

  4. 若要查看作用中的新計時器,請移至程式碼編輯器並將下列程式碼加入至 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 參考變數,以便表單準備讓玩家按下另一個圖示。

  5. 儲存並執行您的程式。 按一下圖示,它會成為可見狀態。

  6. 按一下另一個圖示。 它會短暫出現,然後這兩個圖示會消失。 重複此動作許多次。 表單現在會追蹤您按下的第一個和第二個圖示,並使用計時器在圖示消失之前暫停追蹤。

若要繼續或檢視