1

我正在学习 c# 和 wpf。我有在 Visual Studio 2012 中运行的项目。为了更好地学习 C# 和 WPF,我正在制作一个练习应用程序,它恰好是 Simon Says 游戏。我有两个窗户。第一个窗口有一个“PLAY!” 显示下一个窗口的按钮。此窗口有 4 个按钮。我有一个整数数组,因此玩家必须按顺序按下按钮。为了向玩家显示顺序,我想按照数组中生成的顺序一个一个地为每个按钮设置动画。

这些按钮有 4 种不同的颜色,每个按钮的动画都是我用 Blend 中的 Storyboard 制作的。动画将按钮从其原始颜色变为红色,然后在两秒内返回。

动画正常工作,但所有按钮同时动画。我希望第一个按钮动画,然后当它完成时,下一个,依此类推。我将如何最好地使用 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 回答