我正在尝试使用 WPF 使用 WiiMote 创建一个非常简单的游戏 Simon 版本。我坚持的是如何使它基于转动,程序阻塞,直到 GUI 完成显示序列。
这是我到目前为止的代码(主要基于这里的答案:WPF - 顺序动画简单示例):
public partial class Window1 : Window
{
public enum SimonSquare { BLUE = 1, GREEN = 3, RED = 5, YELLOW = 7 };
List<int> _correctSequence;
int _currentLevel = 1;
Random random = new Random();
Wiimote _wiiMote;
List<int> _squaresEntered;
private IEnumerator<Action> _actions;
Rectangle blueRect;
Rectangle redRect;
Rectangle greenRect;
Rectangle yellowRect;
AutoResetEvent autoEvent;
public Window1()
{
InitializeComponent();
blueRect = new Rectangle() { Fill =
System.Windows.Media.Brushes.Blue, Name = "Blue"};
redRect = new Rectangle() { Fill =
System.Windows.Media.Brushes.Red, Name = "Red" };
greenRect = new Rectangle() { Fill =
System.Windows.Media.Brushes.Green, Name = "Green" };
yellowRect = new Rectangle() { Fill =
System.Windows.Media.Brushes.Yellow, Name = "Yellow" };
UniformGrid1.Children.Add(new Rectangle() { Fill =
System.Windows.Media.Brushes.LightGray });
UniformGrid1.Children.Add(blueRect);
UniformGrid1.Children.Add(new Rectangle() { Fill =
System.Windows.Media.Brushes.LightGray });
UniformGrid1.Children.Add(redRect);
UniformGrid1.Children.Add(new Rectangle() { Fill =
System.Windows.Media.Brushes.LightGray });
UniformGrid1.Children.Add(greenRect);
UniformGrid1.Children.Add(new Rectangle() { Fill =
System.Windows.Media.Brushes.LightGray });
UniformGrid1.Children.Add(yellowRect);
UniformGrid1.Children.Add(new Rectangle() { Fill =
System.Windows.Media.Brushes.LightGray });
//connectWiiRemote();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_actions = AnimationSequence().GetEnumerator();
autoEvent = new AutoResetEvent(false);
Thread thread = new Thread(RunNextAction);
thread.Start();
autoEvent.WaitOne(); // need to block here somehow!
int x = 5;
}
IEnumerable<Action> AnimationSequence()
{
getSequence();
foreach(int square in _correctSequence)
{
if(square == (int) SimonSquare.BLUE)
yield return () => animateCell(blueRect, Colors.Blue);
else if(square == (int) SimonSquare.RED)
yield return () => animateCell(redRect, Colors.Red);
else if (square == (int)SimonSquare.GREEN)
yield return () => animateCell(greenRect, Colors.Green);
else if (square == (int)SimonSquare.YELLOW)
yield return () => animateCell(yellowRect, Colors.Yellow);
}
}
private void animateCell(Rectangle rectangle, Color fromColor)
{
this.Dispatcher.BeginInvoke(new Action(delegate
{
Color toColor = Colors.White;
ColorAnimation ani = new ColorAnimation(toColor,
new Duration(TimeSpan.FromMilliseconds(300)));
ani.AutoReverse = true;
SolidColorBrush newBrush = new SolidColorBrush(fromColor);
ani.BeginTime = TimeSpan.FromSeconds(2);
rectangle.Fill = newBrush;
ani.Completed += (s, e) => RunNextAction();
newBrush.BeginAnimation(SolidColorBrush.ColorProperty, ani);
}));
}
private void RunNextAction()
{
if (_actions.MoveNext())
_actions.Current();
else
{
autoEvent.Set();
_currentLevel++;
}
}
private void getSequence()
{
_correctSequence = new List<int>();
int[] values =
Enum.GetValues(typeof(SimonSquare)).Cast<int>().ToArray();
for (int i = 0; i < _currentLevel + 2; i++)
{
_correctSequence.Add(values[random.Next(values.Length)]);
}
}
}
但是,autoSet 的 waitOne/set 不能正常工作。它当前调用 RunNextAction 一次,但随后无限期地阻塞在 waitOne 上。我究竟做错了什么?
编辑:让我试着改写这个问题。如果我取出 Threading 和 AutoResetEvent,在 Window_Loaded 我有:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_actions = AnimationSequence().GetEnumerator();
RunNextAction(); // shows all of the flashing squares
// need to wait here until the flashing squares are all shown
// process player's events etc.
}
当我运行上面的代码时,它会调用一次 RunNextAction,它会一直调用自己,直到所有的方块都显示出来(它似乎在它自己的线程上),但是 WindowLoaded 方法会继续运行。在我调用 RunNextAction() 之后,我需要 Window_Loaded 阻止,直到 RunNextAction 完全完成。