C# 单游戏:在类中创建绘图函数
我想做一个瓷砖类,其中包含自己的绘图功能。以下是我到目前为止的情况。运行编码器时显示的错误是。System.InvalidOperationException:'在成功调用End之前,无法再次调用Begin。此错误消息显示在spirteBatch上。从类内部开始C# 单游戏:在类中创建绘图函数,c#,monogame,C#,Monogame,我想做一个瓷砖类,其中包含自己的绘图功能。以下是我到目前为止的情况。运行编码器时显示的错误是。System.InvalidOperationException:'在成功调用End之前,无法再次调用Begin。此错误消息显示在spirteBatch上。从类内部开始 namespace TileGame.v3 { 类瓷砖 { } { } 纹理是1920x1080,我已经测试了绘图和切割,效果很好。首先,您的代码可能不完整,但您从未在任何地方调用draw方法 假设你正在调用它,你确定你的纹理大于30
namespace TileGame.v3
{
类瓷砖
{
}
{
}
纹理是1920x1080,我已经测试了绘图和切割,效果很好。首先,您的代码可能不完整,但您从未在任何地方调用draw方法 假设你正在调用它,你确定你的纹理大于300x300吗?对于x,y=300300到x,y=400400,该纹理包含什么?在这些位置有任何颜色数据吗 如果您对所有这些都回答是,我们将需要更多的代码来帮助您
侧边节点:由于这是一个磁贴,您可能要渲染其中的许多磁贴。您不需要为所有磁贴调用.Begin和.End。只需在第一个磁贴之前调用Begin,在最后一个磁贴之后调用End,就可以了。这将使渲染速度大大加快。我不熟悉monogame,但我熟悉unity,难道您的磁贴不应该是c吗lass继承自game类?更新做了什么,因为据我所知,递归语句必须占用您实际调用的所有CPU。Draw anywhere?如果您使用visual studio 2017,您可以启用常见运行时异常并重新运行代码。您可以在异常设置中启用此选项。Shortkey到设置:Crtl+Alt+E。可能会出现错误。您是否真的在任何地方加载纹理并调用绘图函数?您从未在任何地方调用Drawa.texture…是吗?
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);
}
}