Quando uso XNA Framework (per Windows Phone) il mio gioco funziona perfettamente, ma quando eseguo la migrazione su Silverlight/XNA Framework ho riscontrato un problema con il ritardo delle animazioni. Il problema è il seguente: Quando imposto il passaggio temporale fisso a GameTimer (timer.UpdateInterval = TimeSpan.FromTicks (333333)), il passo REAL time non è FISSO e gli eventi timer (OnUpdate, OnDraw) si attivano con intervalli diversi. Questo codice mostra il mio problema in modo più chiaro:Silverlight/XNA animation lagging
Silverlight/XNA Framework: (animazione in ritardo di sviluppo):
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: (tutto funziona benissimo):
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 per la risposta. Ho immaginato il mio errore: ho usato l'animazione basata sulla frequenza di fotogrammi fissa e calcolare l'animazione nel metodo di aggiornamento per indice di fotogrammi. Ora calcolerò l'animazione per il tempo reale trascorso nel metodo Draw. Thx molto. – Ivan