C# 如何在不同的类中使用Monogame GraphicsDeviceManager?
我有一个类,它有一个在屏幕上绘制形状的方法C# 如何在不同的类中使用Monogame GraphicsDeviceManager?,c#,monogame,C#,Monogame,我有一个类,它有一个在屏幕上绘制形状的方法 public class Rectangle : Game1 { Texture2D quadl; public Rectangle() { } public void size() { quadl = new Texture2D(this.GraphicsDevice, 100, 100); } } 然后我在Game1类中调用这个更新方法 Rectangle rt=
public class Rectangle : Game1 {
Texture2D quadl;
public Rectangle() {
}
public void size() {
quadl = new Texture2D(this.GraphicsDevice, 100, 100);
}
}
然后我在Game1类中调用这个更新方法
Rectangle rt=new Rectangle();
rt.size()代码>
然后产生一个无限循环
有什么问题吗?我怎么去修理它呢?
我怀疑它与GraphicsDeviceManager有关,但我没有找到任何帮助。您的矩形不应继承Game1。如果需要访问GraphicsDevice,请将其作为参数传递给构造函数。因为现在,您正在为每个矩形创建一个新的Game1
public class Rectangle {
Texture2D quadl;
private readonly GraphicsDevice _graphicsDevice;
public Rectangle(GraphicsDevice graphicsDevice) {
this._graphicsDevice = graphicsDevice;
}
public void size() {
quadl = new Texture2D(this._graphicsDevice, 100, 100);
}
}
因为我们知道你现在正在做什么,你正在用每个矩形创建一个新的游戏实例,每个矩形都有自己的GraphicsDevice实例