【发布时间】:2014-04-12 19:21:55
【问题描述】:
我有一个由丑陋的 13 个参数组成的方法,但我不知道如何将这些转换为正确的子方法,并且仍然功能完全相同,在使用正确的参数进行三次迭代后停止,并以不同的方法等。有人会在正确的方向上帮助我吗?
//Three part animation
public void SetPlayerAnimation(int location, int x1, int y1, TimeSpan duration1, string sprite1, int x2, int y2, TimeSpan duration2, string sprite2, int x3, int y3, TimeSpan duration3, string sprite3, string endsprite)
{
//Get the sprite object to be animated
TranslateTarget = "Sprite" + location.ToString();
OnPropertyChanged("TranslateTarget");
//Start first part
RunAnimation(location, x1, y1, duration1, sprite1);
var timer = new DispatcherTimer();
timer.Interval = duration1;
timer.Start();
timer.Tick += (s, args) =>
{
//Start second part
RunAnimation(location, x2, y2, duration2, sprite2);
timer.Stop();
var timer2 = new DispatcherTimer();
timer2.Interval = duration2;
timer2.Start();
timer2.Tick += (s2, args2) =>
{
//Start third part
RunAnimation(location, x3, y3, duration3, sprite3);
timer2.Stop();
var timer3 = new DispatcherTimer();
timer3.Interval = duration2;
timer3.Start();
timer3.Tick += (s3, args3) =>
{
//End with a final sprite
SetPlayerSprite(location, endsprite);
timer3.Stop();
};
};
};
}
【问题讨论】:
-
运行一个计时器,或者甚至是一个适当的游戏循环。每次计时器滴答作响或游戏循环跳动时,检查时间,如果有任何内容需要更新,请更新它。
-
嗯,我正在编写的程序是非常程序化的。每 20 秒左右有一次命令,一旦动画被触发,它应该简单地“运行”,它总是完成,并且在它开始后永远不会改变它的行为。完整的游戏循环会不会有点矫枉过正?
标签: c# timer delegates refactoring