参考我目前正在构建的这个编程游戏。
我有一个类库(dll),它将有一个方法,该方法Run
由以下内容组成:
public class MyRobot : Robot
{
public void Run(}
{
while (true)
{
Ahead(200); //moves the bot 200pixels
TurnLeft(90); //turns the bot by 90deg
}
}
}
在这些方法中(继承自Robot
),系统将使用 WPF(使用BeginAnimation
或DispatcherTimer
)为机器人设置动画。
现在,问题是在完成当前方法之前我没有返回方法(即继续下一个方法),因为这将导致动画一起发生,并且在无限循环中时(如上面一个),这尤其不好。
我的问题是,在完成动画之前防止方法返回的最佳方法是什么?
我目前bool
在Robot
类 ( isActionRunning
) 中有一个标记为何true
时开始运行动作,然后false
在动画回调中更改为(Completed
如果使用则使用事件BeginAnimation
)。
在每个方法结束时(调用后BeginAnimation
),我放置了以下循环:
while (isActionRunning)
{
Thread.Sleep(200); //so that the thread sleeps for 200ms and then checks again if the animation is still running
}
这样该方法就不会在动画结束之前返回。
但我觉得这不是正确的做法。
谁能指导我实现这一目标的最佳方法?