/// <summary> /// Creates a new <see cref="Jv.Games.Xna.Sprites.LoopedAnimation"/>. /// </summary> /// <param name="name">Name of the animation</param> /// <param name="frames">Frames to be played sequentially</param> /// <param name="duration">How long it should take to play all frames sequentially.</param> /// <param name="resetToFrame">Index of the first frame to be played, after an animation loop completes.</param> /// <param name="loopCount">How many times the animation should repeat, before completing.</param> public LoopedAnimation(string name, Frame[] frames, TimeSpan duration, int resetToFrame, int? loopCount) : base(name, frames, duration) { if (resetToFrame >= frames.Length || resetToFrame < 0) throw new ArgumentOutOfRangeException("resetToFrame", "ResetToFrame must point to a valid frame index"); if (loopCount < 0) throw new ArgumentOutOfRangeException("loopCount", "Invalid LoopCount quantity"); ResetToFrame = resetToFrame; LoopCount = loopCount; }
/// <summary> /// Creates a new playable animation. /// </summary> /// <param name="name">Name of the animation</param> /// <param name="frames">Frames to be played sequentially</param> /// <param name="duration">How long it should take to play all frames sequentially.</param> public Animation(string name, Frame[] frames, TimeSpan duration) { if (string.IsNullOrEmpty(name)) throw new ArgumentException("Name cannot be null or empty", "name"); if (frames == null) throw new ArgumentNullException("frames"); if (frames.Length <= 0) throw new ArgumentException("Frames array cannot be empty", "frames"); if (duration <= TimeSpan.Zero) throw new ArgumentOutOfRangeException("duration", "Invalid duration"); Name = name; Frames = frames; Duration = duration; Reset(); }
/// <summary> /// Forces the animation to play from beginning of the specified frame index. /// </summary> /// <param name="frameIndex">Frame index to be played.</param> protected void JumpToFrame(int frameIndex) { if (frameIndex < 0) throw new ArgumentOutOfRangeException("frameIndex", "CurrentFrameIndex cannot be negative"); if (frameIndex >= Frames.Length) throw new ArgumentOutOfRangeException("frameIndex", "CurrentFrameIndex cannot point outside the Frames array"); _currentFrameIndex = frameIndex; _currentFrame = Frames[frameIndex]; _currentFrameDuration = TimeSpan.FromSeconds((Duration.TotalSeconds / Frames.Sum(f => f.DurationWeight)) * _currentFrame.DurationWeight); _frameSpentTime = TimeSpan.Zero; }