Share via


手順 6: タイマーの追加

次に、絵合わせゲームにタイマーを追加します。

タイマーを追加するには

  1. Windows フォーム デザイナーのツールボックスに移動します。[Timer] ([コンポーネント] カテゴリ内) をダブルクリックして、フォームにタイマーを追加します。次の図に示すように、タイマーのアイコンが、フォームの下の灰色のボックスに表示されます。

    Timer

    タイマー

  2. [timer1] アイコンをクリックしてタイマーを選択します。[Interval] プロパティを 750 に設定します。ただし、[Enabled] プロパティは [False] のままにします。Interval プロパティは、次に時間を刻むまでに待機する時間をタイマーに指示します。したがって、この場合は、最初の Tick イベントを発生させるまでに 4 分の 3 秒 (750 ミリ秒) 待機するようタイマーに指示します。プログラムの起動時にタイマーが開始されないようにします。代わりに、Start() メソッドを使用して、プレーヤーが 2 つ目のラベルをクリックしたときにタイマーが開始されるようにします。

  3. Windows フォーム デザイナーの 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 イベント ハンドラーは、3 つのことを実行します。最初に、Stop() メソッドを呼び出してタイマーを停止します。次に 2 つの参照変数 firstClicked および secondClicked を使用して、プレーヤーがクリックした 2 つのラベルを取得し、それらのアイコンを再度非表示にします。最後に、firstClicked 参照変数と secondClicked 参照変数を null (Visual C# の場合) または Nothing (Visual Basic の場合) にリセットします。これは、プログラム自体がリセットされるしくみであるため重要です。この時点では、Label コントロールが追跡されておらず、プレーヤーの次の 1 回目のクリックに対する準備ができている状態です。

    注意

    Timer オブジェクトには、タイマーを開始する Start() メソッドと、タイマーを停止する Stop() メソッドがあります。[プロパティ] ウィンドウでタイマーの [Enabled] プロパティを [True] に設定した場合、タイマーはプログラムが起動するとすぐに時間を刻み始めます。一方、[False] に設定したままにした場合、その Start() メソッドが呼び出されるまでは時間の刻みは始まりません。

    注意

    通常、タイマーは、Interval プロパティを使用して、次に時間を刻むまでに待機するミリ秒数を判断し、その Tick イベントを繰り返し発生させます。ここでは、タイマーの Stop() メソッドが Tick イベント内で呼び出されるしくみになっています。これにより、タイマーがワン ショット モードになり、Start() メソッドが呼び出されると、タイマーがその間隔分の時間を待機し、Tick イベントを 1 回発生させるようになります。

  4. 新しいタイマーの動作を確認するには、コード エディターに移動し、label_Click() イベント ハンドラー メソッドの上部と下部に次のコードを追加します (if ステートメントを、上部に 1 つ追加し、下部に 3 つ追加することになります。メソッドの他の部分は同じです)。

    ''' <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 = True) Then
            Return
        End If
    
        Dim clickedLabel As Label = 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
                Return
            End If
    
            ' 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
                Return
            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 プロパティをチェックして、タイマーが開始されているかどうかをチェックします。これにより、プレーヤーが 1 つ目と 2 つ目の Label コントロールをクリックした場合はタイマーが開始され、3 つ目のコントロールをクリックした場合は何も実行されません。

    メソッドの下部のコードは、プレーヤーがクリックした 2 つ目の Label コントロールを追跡し、そのラベルのアイコンの色を黒に設定してアイコンを表示するように、secondClicked 参照変数を設定します。これにより、タイマーがワン ショット モードで開始され、750 ミリ秒待機してから、Tick イベントを発生させるようになります。その後で、タイマーの Tick イベント ハンドラーが 2 つのアイコンを非表示にし、firstClicked 参照変数と secondClicked 参照変数をリセットして、フォームを、プレーヤーが他のアイコンをクリックできる状態にします。

  5. プログラムを保存し、実行します。アイコンをクリックすると、そのアイコンが表示されます。

  6. 他のアイコンをクリックします。そのアイコンが一時的に表示され、その後、両方のアイコンが非表示になります。これを何度も繰り返します。これで、フォームで、クリックした 1 つ目と 2 つ目のアイコンが追跡され、タイマーを使用して、少し時間をおいてからアイコンが非表示にされるようになりました。

続行または確認するには