首先,我不太熟悉编程(17并且还在学习它),很抱歉,如果我的代码不是那么好^^
我在C#中看到过关于2D引擎的东西,并且它们实际上并不难以编码,所以我想试一试,但现在我遇到了问题:
我在表单中创建了一个Panel,然后它被绘制我“提取”图形对象,并将其提供给我的“引擎”
private void panel1_Paint(object sender, PaintEventArgs e)
{
Console.WriteLine("Draw");
graphics.setG(e.Graphics);
}
引擎只是“保存”图形对象
public void setG(Graphics g)
{
this.g = g;
}
现在我有了通常的游戏循环,每1/60秒执行一次更新和绘图方法,部分绘制方法:
for(int i = 0;i < images.Count; i++)
{
var curr = images[i];
g.DrawImage(curr.img, (int)Math.Round(curr.x), (int)Math.Round(curr.y));
}
当然img是一个图像,x和y是浮点数,转换为整数,现在我得到“未处理的异常类型'System.DrgumentException'在System.Drawing.dll中出现”在行
g.DrawImage(curr.img, (int)Math.Round(curr.x), (int)Math.Round(curr.y));
异常细节(尝试翻译几个单词,原始日志是德语):
System.ArgumentException was unhandled
HResult=-2147024809
Message=Invalid Parameter.
Source=System.Drawing
StackTrace:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y)
at _2D_Engine.GraphicsEngine.Draw(Graphics g) in C:\Users\Paul\Documents\Visual Studio 2015\Projects\2D Engine\2D Engine\GraphicsEngine.cs:Line 38.
at _2D_Engine.Form1.gameLoop() in C:\Users\Paul\Documents\Visual Studio 2015\Projects\2D Engine\2D Engine\Form1.cs:Line 54.
at _2D_Engine.Form1.gameLoop() in C:\Users\Paul\Documents\Visual Studio 2015\Projects\2D Engine\2D Engine\Form1.cs:Line 58.
at _2D_Engine.Form1.gameLoop() in C:\Users\Paul\Documents\Visual Studio 2015\Projects\2D Engine\2D Engine\Form1.cs:Line 58.
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
这种情况发生在我试图绘制到Panel的任何地方,当我尝试绘制Polygon时,我得到相同的异常。不确定我是否误解了如何编写游戏循环,因为当我从Paint方法调用draw方法时不会发生,但是每次我想要一个对象移动时都不会调用Paint方法,所以我不能只用它来绘制循环,我现在很困惑^^