我正在爲XNA的Animation類工作。我決定嘗試使用SpriteBatch的擴展方法來添加可以繪製AnimatedSprite的Draw方法。XNA SpriteBatch繪製擴展方法拋出ArrayTypeMismatchException
AnimatedSprite包含一個Sprite對象列表,而一個Sprite對象具有一個Texture2D圖像。 我創建了一個靜態擴展類,其中包含Draw方法的不同重載。
這裏有一個這樣的方法(我假設的問題是針對所有的功能類似):
public static void Draw(this SpriteBatch b, AnimatedSprite sprite, Vector2 position, Color color)
{
b.Draw(sprite.CurrentSprite.Image, position, color);
}
我得到一個ArrayTypeMismatchException
當我跑我的項目。我不確定是什麼導致它,因爲sprite.CurrentSprite.Image
是一個Texture2D。
希望有人能夠幫助我理解是什麼導致了這個問題和潛在的問題。
謝謝!
更新:AnimatedSprite類
public class AnimatedSprite
{
List<Sprite> frames;
double elapsedSeconds;
int currentIndex;
/// <summary>
/// CurrentSprite is the current frame the animation is on
/// Use CurrentSprite.Image to get the underlying Texture2D
/// </summary>
public Sprite CurrentSprite { set; get; }
/// <summary>
/// FrameRate is the frame rate of the animation, measured
/// in frames per second
/// </summary>
public int FrameRate { set; get; }
bool shouldAnimate;
/// <summary>
/// Constructor for AnimatedSprite
/// </summary>
/// <param name="sprites">A list of sprites, cannot be left null</param>
/// <param name="frameRate">The framerate in frames per second</param>
/// <exception cref="ArgumentException">Thrown if sprites is null</Sprite></exception>
public AnimatedSprite(List<Sprite> sprites, int frameRate)
{
if (sprites == null) throw new ArgumentException("Please provide a non-null List<Sprite>");
this.frames = sprites;
this.currentIndex = 0;
if(this.frames.Count != 0) this.CurrentSprite = frames[currentIndex];
this.FrameRate = frameRate;
this.elapsedSeconds = 0;
}
/// <summary>
/// Add a frame to the animation. It will be added to the end.
/// </summary>
/// <param name="frame">The Sprite you would like to add</param>
public void AddFrame(Sprite frame)
{
frames.Add(frame);
if (frames.Count == 1) this.CurrentSprite = frames[0];
}
/// <summary>
/// Call this function in any Update method to continue the animation
/// </summary>
/// <param name="gameTime">The gameTime object keeping track of time</param>
public void Update(GameTime gameTime)
{
elapsedSeconds += gameTime.ElapsedGameTime.TotalSeconds;
if (elapsedSeconds > (1.0/FrameRate) && shouldAnimate)
{
NextFrame();
elapsedSeconds = 0;
}
}
#region Animation Controls
/// <summary>
/// Starts the animation. This MUST be called to start the animation
/// </summary>
public void Start()
{
shouldAnimate = true;
}
/// <summary>
/// Stops the animation. Call Start() to start again
/// </summary>
public void Stop()
{
shouldAnimate = false;
}
/// <summary>
/// Advances the animation to the next cell
/// </summary>
public void NextFrame()
{
if (frames.Count == 0) return;
currentIndex++;
if (currentIndex >= frames.Count) currentIndex = 0;
this.CurrentSprite = frames[currentIndex];
}
/// <summary>
/// Advances the animation to the previous cell
/// </summary>
public void PreviousFrame()
{
if (frames.Count == 0) return;
currentIndex--;
if (currentIndex < 0) currentIndex = frames.Count - 1;
this.CurrentSprite = frames[currentIndex];
}
#endregion Animation Controls
你可以發佈AnimatedSprite類嗎? – Pidalia 2011-01-27 12:18:50