[英]XNA SpriteBatch Draw Extension Method throws ArrayTypeMismatchException
我正在為XNA制作動畫課。 我決定嘗試為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類
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
使用的唯一數組是sprite.CurrentSprite。 這個(我假設)使用您存儲在動畫Sprite類中的內部索引來返回Sprite數組中該索引處的Sprite。 由於這是您使用數組的唯一位置,因此這是您的問題。 由於您尚未發布AnimatedSprite類代碼,因此我只建議您查看AnimatedSprite類中的CurrentImage屬性,看看它是否訪問Sprite []。 請發布AnimatedSprite類,以便為您提供更多幫助。
另外,以我個人的觀點,您不應在AnimatedSprite中存儲sprite類的實例,而應僅存儲紋理數組。 存儲一個Sprite的數組意味着每個Sprite都是獨立的實體,它們不是。 它們是同一個動畫精靈的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.