當我使用XNA Framework(對於Windows Phone)時,我的遊戲完美運行,但是當我在Silverlight/XNA Framework上遷移時,我遇到了動畫滯後問題。 問題如下: 當我將固定時間步長設置爲GameTimer(timer.UpdateInterval = TimeSpan.FromTicks(333333))時,REAL時間步不會以固定時間步長(OnUpdate,OnDraw)以不同的時間間隔觸發。 這個代碼顯示了我的問題更清楚:Silverlight/XNA動畫滯後
的Silverlight/XNA框架:(動畫滯後):
TimeSpan curNow;
TimeSpan lastUpdate;
TimeSpan lastDraw;
public GamePage()
{
timer = new GameTimer();
timer.UpdateInterval = TimeSpan.FromTicks(333333);
timer.Update += OnUpdate;
timer.Draw += OnDraw;
}
private void OnUpdate(object sender, GameTimerEventArgs e)
{
curNow = new TimeSpan(DateTime.Now.Ticks);
TimeSpan elapsed=e.ElapsedTime;//Always constant and has value: 33ms
TimeSpan realElapsed = curNow - lastUpdate;//Real elapsed time always changing and has a value between: 17-39ms (sometimes more then 39ms)
lastUpdate = curNow;
}
private void OnDraw(object sender, GameTimerEventArgs e)
{
curNow = new TimeSpan(DateTime.Now.Ticks);
TimeSpan elapsed=e.ElapsedTime;//Always changing and has a value between: 17-39ms (sometimes more then 39ms)
TimeSpan realElapsed = curNow -lastDraw;//Always changing and has a value between: 17-39ms (sometimes more then 39ms)
lastDraw= curNow;
}
XNA框架:(所有工作正常):
TimeSpan curNow;
TimeSpan lastUpdate;
TimeSpan lastDraw;
public Game()
{
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
}
protected override void Update(GameTime gameTime)
{
curNow = new TimeSpan(DateTime.Now.Ticks);
TimeSpan elapsed=gameTime.ElapsedGameTime;//Always constant and has value: 33ms
TimeSpan realElapsed = curNow - lastUpdate;//Real elapsed time has a value between: 34-35ms (sometimes more then 35ms)
lastUpdate = curNow;
}
protected override void Draw(GameTime gameTime)
{
curNow = new TimeSpan(DateTime.Now.Ticks);
TimeSpan elapsed=gameTime.ElapsedGameTime ;//Value between: 33-34ms (sometimes more then 34ms)
TimeSpan realElapsed = curNow - lastDraw;//Value between: 34-35ms (sometimes more then 35ms)
lastDraw = curNow;
}
Thx回覆。 我想我的錯誤:我使用基於固定幀速率的動畫並通過幀索引在Update方法中計算動畫。 現在我將在Draw方法中以實際經過的時間計算動畫。 Thx很多。 – Ivan 2012-04-20 09:56:05