不知道为什么我得到“非静态字段需要对象引用”

时间:2021-11-16 11:32:12

My code is below. Any help would be appreciated. I apologize in advance if it's a simple mistake - I'm still a beginner at C# (and object oriented in general).

我的代码如下。任何帮助,将不胜感激。如果这是一个简单的错误,我会提前道歉 - 我仍然是C#的初学者(面向对象)。

public class Game1 : Microsoft.Xna.Framework.Game
{
    public void Update()
    {
        Levels level = new Levels();
        Game1 mainGame = Game1;
        Levels.AliensSetUp(1, 2, 50, 0, 0, 0, mainGame);
    }
}

class Levels
{
    public void AliensSetUp(int numberRows, int numberColumns, int spaceBetween, int XStart, int YStart, int AlienType, Game1 mainGame)
}

The error is on the "mainGame" in "Levels.AliensSetUp(1, 2, 50, 0, 0, 0, mainGame)". I've also tried replacing "mainGame" with just "game1" but I get an error for that too. Thank you for any help you can give me.

错误发生在“Levels.AliensSetUp(1,2,50,0,0,0,mainGame)”中的“mainGame”上。我也尝试用“game1”替换“mainGame”,但我也得到了一个错误。感谢您给我的任何帮助。

1 个解决方案

#1


3  

Levels is not static. You have "new" up the Levels class and then call its AliensSetUp() method. Ex:

级别不是静态的。你有Levels类的“new”,然后调用它的AliensSetUp()方法。例如:

var levels = new Levels(); 
levels.AliensSetup(...)

#1


3  

Levels is not static. You have "new" up the Levels class and then call its AliensSetUp() method. Ex:

级别不是静态的。你有Levels类的“new”,然后调用它的AliensSetUp()方法。例如:

var levels = new Levels(); 
levels.AliensSetup(...)