【问题标题】:MonoGame GetData implemented exceptionMonoGame GetData 实现异常
【发布时间】:2013-11-28 07:01:32
【问题描述】:

我刚开始学习 XNA/MonoGame,遇到了一个奇怪的异常。
错误说:The method or operation is not implemented.
更奇怪的是,几乎相同的非常相似的代码可以工作。唯一不同的是,其他代码运行在 XNA 和另一台计算机上,而我的代码运行在 MonoGame 上。

代码应该从精灵表制作动画。

在我的课堂上,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]);
            }
        }
    }

背后的想法是在精灵表下方有一行点,我可以用这些点从精灵表中制作框架。 这是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 中。 (The method or operation is not implemented.)当我按右键时。为什么会这样?

【问题讨论】:

  • 问题出在哪里?
  • @pinckerman 对不起,我的意思是问为什么会发生这个错误。编辑它。无论如何,我得到了答案,MonoGame 的错误。
  • 是的,如果您不为 Windows 部署,GetData 在 Monogame 上存在已知问题。
  • 您的目标平台是什么?在移动设备上获取纹理数据将是一个非常缓慢的操作,所以即使你确实让它以这种方式工作,它可能仍然是一个问题。您是否考虑过以另一种方式阅读位图?或者也许将数据存储为坐标列表。

标签: c# xna monogame


【解决方案1】:

老实说,这在 XNA 和它的一个端口之间是一个很大的区别。

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

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

但使用 SharpDX 版本,此错误不存在。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-25
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多