我试图在另一个类中使用在Game1.cs
中声明的变量。如何在MonoGame的其他类中使用Game1类中声明的变量
为了简化,我创建了一个新项目,并且想操作int变量x。 我得到Object reference not set to an instance of an object.
在game.x = 0;
我是新的C#,谁能告诉我是什么问题,如何解决?
这是类:
class Class1
{
Game1 game;
public void init()
{
game = new Game1();
}
public void Update()
{
game.x = 0;
game.x++;
}
}
这是我的代码在Game1.cs
public int x = 0;
Class1 y = new Class1();
//other stuff
protected override void Initialize()
{
y.init();
base.Initialize();
}
// Other stuff...
protected override void Update(GameTime gameTime)
{
y.Update();
base.Update(gameTime);
}
为什么要以这种方式访问game1?你的目标是什么? – jgallant
思考之后,我想我并不需要这样使用它。相反,我会将所需的变量作为参数发送给另一个类。谢谢。 – Val