When I use the XNA Framework (for Windows Phone), my game works fine, but when I migrate to Silverlight / XNA Framework, I had a problem with animation delay. The problem is this: When I set a fixed time step in GameTimer (timer.UpdateInterval = TimeSpan.FromTicks (333333)), the REAL time step is not FIXED, and timer events (OnUpdate, OnDraw) fire at different intervals. This code more clearly shows my problem:
Silverlight / XNA Framework: (animation delay):
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;
XNA Framework: (everything 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; }
source share