gpt4 book ai didi

c# - Silverlight/XNA 动画滞后

转载 作者:太空狗 更新时间:2023-10-29 21:29:15 26 4
gpt4 key购买 nike

当我使用 XNA Framework(对于 Windows Phone)时,我的游戏运行完美,但是当我迁移到 Silverlight/XNA Framework 时,我遇到了动画滞后的问题。问题如下:当我将固定时间步长设置为 GameTimer (timer.UpdateInterval = TimeSpan.FromTicks(333333)) 时,实际时间步长不固定并且计时器事件(OnUpdate、OnDraw)以不同的时间间隔触发。此代码更清楚地显示了我的问题:

Silverlight/XNA Framework:(动画滞后):

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;
}

最佳答案

更改为

TimeSpan.Zero;

是的,我知道这感觉很奇怪,而且确实如此。从一些官方游戏指南中得到它。除非我使用它,否则我的游戏会像疯了似的滞后。

关于c# - Silverlight/XNA 动画滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10241597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com