【问题标题】:Monogame: Creating Draw Function In ClassMonogame:在类中创建绘图函数
【发布时间】:2019-01-29 19:07:27
【问题描述】:

我想做一个包含它自己的绘图功能的瓷砖类。以下是我到目前为止所拥有的。我运行编码器时显示的错误是。 “System.InvalidOperationException:'在成功调用 End 之前无法再次调用 Begin。”此错误消息显示在类中的 spirteBatch.Begin() 中。

namespace TileGame.v3

{ 类瓷砖 {

    public Texture2D texture { get; set; }
    public int X { get; set; }
    public int Y { get; set; }
    public int sourceX { get; set; }
    public int sourceY { get; set; }
    public int width { get; set; }
    public int height { get; set; }



    public void Draw(GameTime gameTime, SpriteBatch spriteBatch)

    {

        spriteBatch.Begin();

        spriteBatch.Draw(texture, new Vector2(X, Y), new Rectangle(sourceX, sourceY, width, height), Color.White);

        spriteBatch.End();

        Draw(gameTime, spriteBatch);
    }

}

}

namespace TileGame.v3

{

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D mark;
    Texture2D peep1;
    Texture2D ocean;

    int x1 = 100;
    int y1 = 100;
    int x2 = 400;
    int y2 = 400;
    int tempx;
    int tempy;

    bool win = false;

    Tile a = new Tile();



    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }



    protected override void Initialize()
    {


        base.Initialize();
    }


    protected override void LoadContent()
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);

        mark = Content.Load<Texture2D>("mark");
        peep1 = Content.Load<Texture2D>("peep1");
        ocean = Content.Load<Texture2D>("ocean");

        a.texture = ocean;

    }


    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }


    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();


        if (Keyboard.GetState().IsKeyDown(Keys.Enter))
        {
            tempx = x1;
            x1 = x2;
            x2 = tempx;

            tempy = y1;
            y1 = y2;
            y2 = tempy;

            win = true;
        }

        a.X = 100;
        a.Y = 100;
        a.sourceX = 300;
        a.sourceY = 300;
        a.width = 100;
        a.height = 100;



        base.Update(gameTime);
    }




    protected override void Draw(GameTime gameTime)
    {


        spriteBatch.Begin();

        a.Draw(gameTime, spriteBatch);

        spriteBatch.End();


        base.Draw(gameTime);
    }
}

}

纹理是 1920x1080,我已经测试过绘制和切割它,效果很好。

【问题讨论】:

  • 我不熟悉monogame,但我熟悉unity,你的Tile类不应该继承自游戏类吗? Update() 做了什么,因为据我所知,这是一个递归语句,它必须吃掉你所有的 cpu
  • 你真的在任何地方打电话给a.Draw()吗?
  • 如果您使用的是 Visual Studio 2017,您可以启用 Common Runtime Exceptions 并重新运行您的代码。您可以在Exception Settings 中启用此选项。 (设置快捷键:Crtl+Alt+E)。也许到时候就会出错。
  • 您实际上是在加载纹理并在任何地方调用该绘制函数吗?
  • 你永远不会在任何地方调用 Draw(a.Texture...),是吗?

标签: c# monogame


【解决方案1】:

首先,您的代码可能不完整,但您从未在任何地方调用 draw 方法。

假设你正在调用它,你确定你的纹理大于 300x300 吗?对于 (x,y = 300,300 到 x,y = 400,400),该纹理包含什么?那些位置有颜色数据吗?

如果您对所有这些都回答“是”,我们将需要更多代码来帮助您。

侧节点:由于这是一个图块,您可能要渲染其中的许多图块。您不需要为所有这些调用 .Begin() 和 .End() 。只需在第一个图块之前调用 Begin() 并在最后一个图块之后调用 End() 即可。这将使渲染速度更快。

【讨论】:

    猜你喜欢
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多