我需要在我的OpenGL游戏中将我的精灵移动到每像素路径(List<Point>
轨道)像素,所以我需要实现一种控制时间的方法。我读了不同的方法:使用OnRenderUpdate
(但我不能做任何事情比1/fps更快),使用不同的线程来控制时间或使用StopWatch
(而不是Timer
)。我想用最后一个:定时移动精灵
private void Sprite_Move(Sprite sprite, List<Point> path) {
track.Clear();
for (int i = 1; i < path.Count; i++) {
//memorize all track pixel, so I can update pixel per pixel my sprite position
Bresenham2(path[i-1], path[i]);
}
//moving flag
sprite.moving = true;
//start my StopWatch
timer.Start();
Loop(timer, sprite);
}
private void Loop(Stopwatch timer, Sprite sprite) {
int i = 1;
while (timer.IsRunning) {
//moving each 0.03s?
if ((sprite.moving) & (timer.Elapsed.Milliseconds >= 30)) {
if (i < track.Count) {
//change sprite position to the i-th track pixel
Sprite_Position(skinny, track[i]);
i++;
timer = StopWatch.StartNew();
Console.WriteLine("Tick");
}
//if I'm on the end of the track I can exit
else timer.Stop();
}
}
}
我看不到我的精灵正在移动。一切都被阻塞,直到最后一次迭代,然后我看到我的精灵在轨道的最后一个像素......瞬间!
“OnRenderUpdate”是你应该使用的。 – asawyer 2012-07-31 13:00:33
如果我强迫我的游戏以30 fps运行,我无法做出比1/fps更快的移动。 – 2012-07-31 13:06:46
这没有任何意义。 – asawyer 2012-07-31 13:07:42