【发布时间】: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...),是吗?