gpt4 book ai didi

c# - MonoGame GetData 实现异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:33:23 24 4
gpt4 key购买 nike

我刚开始学习XNA/MonoGame,遇到了一个奇怪的异常。
错误提示:未实现方法或操作。
更奇怪的是,一个非常相似、几乎相同的代码却能工作。唯一的区别是其他代码在 XNA 和另一台计算机上运行,​​而我的代码在 MonoGame 上运行。

代码应该从 sprite 表制作动画。

在我的类(class)中,Animate:

    public void set_state(string name, string state)
{
this.name = name;
this.state = state;
}

public void animate(int frameIndex)
{
this.frameIndex = frameIndex;
this.animatedTexture = Game1.contentManager.Load<Texture2D>(name + '/' + state);

prepare_frames();
while (this.frameIndex > rectangles.Count )
{
frameIndex = frameIndex - rectangles.Count;
}
base.draw(animatedTexture, rectangles[frameIndex], origins[frameIndex]);
}

public void prepare_frames()
{
find_dots();
find_rectangles();
find_origins();
}

public void find_dots()
{
cols = new Color[animatedTexture.Width];
lowestRectangle = new Rectangle(0, animatedTexture.Height - 1, animatedTexture.Width, 1);

animatedTexture.GetData<Color>(0, lowestRectangle, cols, 0, animatedTexture.Width);
for (int i = 0; i < cols.Length; i++)
{
if (cols[i] == Color.Black)
{
dots.Add(new Vector2(i, animatedTexture.Height));
}
}
}

public void find_rectangles()
{
for (int i = 0; i < dots.Count-2; i+=2)
{
rectangles.Add(new Rectangle((int)dots[i].X, 0, (int)dots[i+2].X - (int)dots[i].X, animatedTexture.Height-1));
}
}

public void find_origins()
{
for (int i = 1; i < dots.Count; i++)
{
if (i%2 != 0)
{
origins.Add(dots[i]);
}
}
}

背后的想法是在 sprite 表下面有一排点,通过这些点我可以从 sprite 表制作帧。这是 Animated 类的数据:

    #region data

Texture2D animatedTexture;
string name, state; // to determine the animated state.

Rectangle lowestRectangle; // is the rectangle of the dot's.
Color[] cols; // this array is for the colors on the dot's rectungle.
List<Vector2> dots = new List<Vector2>(); // this list is for the dot's coordinates on the dot's rectungle.
List<Rectangle> rectangles = new List<Rectangle>(); // this list is for the new rectungles, each rectungle is a diffirent frame from the sprite sheet.
List<Vector2> origins = new List<Vector2>(); // this list is for each origin point of the new retungles.

int frameIndex;

#endregion

这是调用上述方法的部分,在 MonoGame 的主类 Game1 中:

    protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();

if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
player.set_state("moshe", "run");
player.animate(frameIndex);
frameIndex++;
}
else
player.draw();

spriteBatch.End();

base.Draw(gameTime);
}

所以错误发生在这一行:

animatedTexture.GetData<Color>(0, lowestRectangle, cols, 0, animatedTexture.Width); 

Animate 类的方法 animate 中。 (方法或操作未实现。)当我按下右键时。为什么会这样?

最佳答案

老实说,XNA 和它的一个端口之间有很大的不同。

A NotImplementedException完全按照它的建议进行,当方法或操作尚未实现时抛出。

似乎没有太多关于此的记录,但已将其报告给MonoGame bug tracker并分配给 3.x 版本的开发人员。

但是使用SharpDX版本,这个错误不存在。

关于c# - MonoGame GetData 实现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com