When I use XNA Framework (For windows Phone) my game work perfectly, but when I migrate on Silverlight/XNA Framework I got a problem with animation lagging. The problem is the following: When I set fixed time step to GameTimer (timer.UpdateInterval = TimeSpan.FromTicks(333333)), the REAL time step doesn't FIXED and timer events (OnUpdate, OnDraw) fires with different intervals. This code shows my problem more clearly:
Silverlight/XNA Framework: (Animation lagging):
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 Framework: (All works fine):
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;
}
Change to
TimeSpan.Zero;
Yes I know this feels weird and it very much is. Got it from some official game guide. Unless I use this, my game lags like crazy.
Based on many years in (and out) of the video games industry: you should not animate against a fixed frame rate in Silverlight/XNA/Mobile.
If possible, change your app to run solely off the game time, rather the operate against an assumed fixed frame-rate (which you will never get on a mobile device - at least until they are a lot more powerful).
The reason XNA game methods take a GameTime
is because you cannot rely on the frame rate... ever.
Don't fight city hall... go with the standards. Make your app as fast as possible (e.g. if your animations skip too far in a frame), or slow down your animation rate to suit the platform (i.e. slow down your game/app).
Note: The reason why Silverlight animation usually looks smoother than Flash animation is because everything is time based and interpolated, not fixed frame.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With