1

私はc#とwpfを学んでいます。Visual Studio 2012 でプロジェクトを実行しています。C# と WPF をよりよく学ぶために、Simon Says ゲームである練習用アプリケーションを作成しています。私は2つの窓を持っています。最初のウィンドウには「PLAY!」があります。次のウィンドウを表示するボタン。このウィンドウには 4 つのボタンがあります。プレーヤーが順番にボタンを押す必要があるように、整数の配列があります。プレーヤーに順序を示すために、配列で生成された順序で各ボタンを 1 つずつアニメーション化したいと考えました。

ボタンは 4 つの異なる色で、それぞれのアニメーションは Blend のストーリーボードから作成しました。アニメーションにより、ボタンが元の色から赤に変わり、2 秒間かけて元の色に戻ります。

アニメーションは正常に動作しますが、すべてのボタンが同時にアニメーション化されます。最初のボタンをアニメーション化し、それが終了したら次のボタンなどを動かしたかったのです。C# と WPF を使用してこれを行うにはどうすればよいでしょうか。助けてくれてありがとう。必要に応じてコードをアップロードできます。

アニメーションを実行する関数

public void startbtn_Click(object sender, RoutedEventArgs e)
{
    // Show the animation sequence for each button
    // up until the current level. (getCurrentLevel is currently set to 5)
    for (int i = 0; i <= engine.getCurrentLevel(); i++)
    {
        // engine.animate(i) returns the button at sequence i to animate
        animate(engine.animate(i));
    }
}

private void animate(int index)
{
    // Storyboard for each button is in the format of ButtonAnimation_INDEX where INDEX is 1, 2, 3 or 4
    Storyboard btnAnimation = (Storyboard)this.Resources["ButtonAnimation_" + index];
    if (btnAnimation != null)
    {
        btnAnimation.Begin();   
    }
}
4

1 に答える 1

0

Completedイベントを使用してそれを理解しました。現在のシーケンスが現在のレベルよりも低いかどうかを確認する関数があります。その場合は、別のボタンをアニメーション化できることを意味します。したがって、完了したイベントは、アニメーション化する次のボタンを使用して関数を再度実行します。

public void startbtn_Click(object sender, RoutedEventArgs e)
{
    engine.resetSequence(); // Start from sequence 0
    // Animate first button
    animate(engine.animate()); 
}


private void animate(int index)
{

    // Storyboard for each button is in the format of ButtonAnimation_INDEX where INDEX is 1, 2, 3 or 4
       Storyboard btnAnimation = (Storyboard)this.Resources["ButtonAnimation_" + index];
       // Added completed event function handler
       btnAnimation.Completed += btnAnimation_Completed;
       if (btnAnimation != null)
       {
           btnAnimation.Begin();   
        }
}


void btnAnimation_Completed(object sender, EventArgs e)
{
     // If another button can be animated in the sequence
     if (engine.CurrentSequence() < engine.getCurrentLevel())
     {
         // Increment the sequence position
         engine.nextSequence();
         // Run the animation with the next button
         animate(engine.animate());
     }
}
于 2012-08-26T18:10:16.983 に答える