XNA SpriteBatch Draw Extension方法抛出ArrayTypeMismatchException

时间:2010-10-17 19:46:32

标签: c# xna extension-methods

我正在为XNA开发一个Animation类。我决定尝试使用SpriteBatch的扩展方法来添加可以绘制AnimatedSprite的Draw方法。

AnimatedSprite包含Sprite对象列表,Sprite对象包含Texture2D图像。 我创建了一个静态Extensions类,它包含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 Class

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

1 个答案:

答案 0 :(得分:0)

使用的唯一数组是sprite.CurrentSprite。这(我假设)使用您存储在动画精灵类中的内部索引来返回精灵数组中该索引处的精灵。由于这是您使用数组的唯一地方,因此这是您的问题。由于您尚未发布AnimatedSprite类代码,我只能建议您查看AnimatedSprite类中的属性CurrentImage,看看它是否访问Sprite []。请发布AnimatedSprite课程,以便我可以为您提供更多帮助。

另外,在我个人看来,你不应该在AnimatedSprite中存储精灵类的实例,你应该只存储一组纹理。存储一个Sprite数组意味着每个sprite都是独立的实体,它们不是;他们是同一个动画精灵的一部分。