我想制作一个包含其自己的绘图功能的tile类。以下是到目前为止的内容。运行编码器时显示的错误是。 “ 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,我已经测试过绘图和剪切,效果很好。
答案 0 :(得分:1)
首先,您的代码可能不完整,但是您永远不会在任何地方调用draw方法。
假设您正在调用它,确定您的纹理大于300x300吗?对于(x,y = 300,300 to x,y = 400,400),该纹理包含什么?在这些位置上是否有任何颜色数据?
如果您对所有这些回答都是肯定的,我们将需要更多代码来帮助您。
侧节点:由于这是一个图块,因此您可能会渲染其中的许多图块。您不需要为所有它们都调用.Begin()和.End()。只需在第一个图块之前调用Begin(),在最后一个图块之后调用End(),就可以了。这将使渲染速度更快。