I have an app which moves a car through specified locations on a map control. I'm using below code to achieve this functionality. But now I want to set a duration for this. I mean the car should reach at a particular time from start to endpoint. How can I do it. I'm passing the duration to timer as shown in the code.
public class PathAnimation
{
private const int _delay = 30;
private DispatcherTimer _timerId;
private int? _duration;
private int _frameIdx = 0;
private bool _isPaused;
private DateTime _timerStart = DateTime.Now;
public PathAnimation(IntervalCallback intervalCallback,int? duration)
{
_duration = duration;
_timerId = new DispatcherTimer();
_timerId.Interval = new TimeSpan(0, 0, 0, 0, _delay);
_timerId.Tick += (s, a) =>
{
if (!_isPaused)
{
if (intervalCallback != null)
{
intervalCallback(AppGlobals._intervalLocs[_frameIdx], AppGlobals._intervalIdx[_frameIdx], _frameIdx);
}
if ((DateTime.Now - _timerStart).TotalMilliseconds >= _duration.Value)
{
_timerId.Stop();
}
_frameIdx++;
}
};
}
public delegate void IntervalCallback(BasicGeoposition loc, int pathIdx, int frameIdx);
public void Play()
{ _timerStart = DateTime.Now;
_frameIdx = 0;
_isPaused = false;
_timerId.Start();
}
public void Pause()
{
_isPaused = true;
}
public void Stop()
{
if (_timerId.IsEnabled)
{
_frameIdx = 0;
_isPaused = false;
_timerId.Stop();
}
}
}
But now what happens is car reaches faster than the duration time and causes an out of index error on intervalCallback(AppGlobals._intervalLocs[_frameIdx], AppGlobals._intervalIdx[_frameIdx], _frameIdx);