2010-10-17 88 views
0

我正在爲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 
+0

你可以發佈AnimatedSprite類嗎? – Pidalia 2011-01-27 12:18:50

回答

0

使用的唯一陣列是sprite.CurrentSprite。這個(我假設)使用你存儲在動畫sprite類中的內部索引來返回sprite數組中該索引處的精靈。由於這是你使用數組的唯一地方,所以這是你的問題。由於您尚未發佈AnimatedSprite類代碼,因此我只能建議您查看AnimatedSprite類中的屬性CurrentImage並查看它是否訪問Sprite []。請發佈AnimatedSprite類,以便我可以幫助您更多。

另外,我個人認爲,你不應該在AnimatedSprite中存儲sprite類的實例,你應該只存儲一個紋理數組。存儲一個Sprite的數組意味着每個Sprite都是獨立的實體,它們不是;他們是同一個動畫精靈的一部分。