C# XNA 非静态字段、方法或 属性 需要对象引用
C# XNA An object reference is required for the non-static field, method, or property
我正在尝试创建产卵器,但出现此错误。
试图修复此错误,但不幸的是我不能。
我知道 XNA Framework 已经过时了,但我用它来学习。
有人愿意帮助我吗?
谢谢
代码:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
int screenWidth;
int screenHeight;
List<Eggs> eggList = new List<Eggs>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.IsFullScreen = false;
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 800;
Content.RootDirectory = "Content";
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
}
public class Eggs
{
public Texture2D texture;
public Vector2 position;
public Vector2 velocity1;
public bool isVisible = true;
Random random = new Random();
int randX;
public Eggs(Texture2D newTexture, Vector2 newPosition)
{
texture = newTexture;
position = newPosition;
randX = random.Next(0, 400);
velocity = new Vector2(randX, 0);
}
public void Update(GraphicsDevice graphic)
{
position += velocity;
if(position.Y < 0 - texture.Height);
isVisible = false;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
float spawn = 0;
protected override void Update(GameTime gameTime)
{
spawn += (float)gameTime.ElapsedGameTime.TotalSeconds;
foreach(Eggs eggList in eggList)
eggList.Update(graphics.GraphicsDevice);
LoadEggs();
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
Exit();
base.Update(gameTime);
}
public void LoadEggs()
{
if(spawn >= 1)
{
spawn = 0;
if(eggList.Count() < 4)
eggList.Add(new Eggs(Content.Load<Texture2D>("Images/egg"), new Vector2(50, 0)));
}
for(int i = 0; i < eggList.Count; i++)
if(!eggList[i].isVisible)
{
eggList.RemoveAt(i);
i--;
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.LightYellow);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
foreach(Eggs eggList in eggList)
{
Eggs.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
为什么会出现这个错误?
error CS0120: An object reference is required for the non-static field, method, or property 'Game1.E
ggs.Draw(SpriteBatch)'
似乎从末尾开始的第 6 行出现问题。
Eggs.Draw(SpriteBatch)
不能这么叫。由于 Eggs 不是静态的 class,Draw 也不是静态方法,这意味着您需要 Eggs 类型的对象来调用方法 Draw。
所以需要这样的东西:
var egg = new Eggs();
egg.Draw(SpriteBatch);
此外,foreach 循环没有意义,不要对项目使用相同的名称,因为它是您正在循环的集合的名称。
绘制中的 foreach 循环:
foreach(Eggs eggList in eggList)
{
Eggs.Draw(spriteBatch);
}
应改为:
foreach(Eggs egg in eggList)
{
egg.Draw(spriteBatch);
}
正如 Luka 所说,它目前直接从 class 调用,通常只适用于静态 classes 和方法。
我只是认为没有必要声明一个新变量,因为您想遍历列表中的鸡蛋,而不是创建一个空 class。所以现在它从 foreach 中的 egglist
声明一个 egg
,并使用那个 egg
调用内部的 draw 函数。
我正在尝试创建产卵器,但出现此错误。
试图修复此错误,但不幸的是我不能。
我知道 XNA Framework 已经过时了,但我用它来学习。
有人愿意帮助我吗?
谢谢
代码:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
int screenWidth;
int screenHeight;
List<Eggs> eggList = new List<Eggs>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.IsFullScreen = false;
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 800;
Content.RootDirectory = "Content";
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
}
public class Eggs
{
public Texture2D texture;
public Vector2 position;
public Vector2 velocity1;
public bool isVisible = true;
Random random = new Random();
int randX;
public Eggs(Texture2D newTexture, Vector2 newPosition)
{
texture = newTexture;
position = newPosition;
randX = random.Next(0, 400);
velocity = new Vector2(randX, 0);
}
public void Update(GraphicsDevice graphic)
{
position += velocity;
if(position.Y < 0 - texture.Height);
isVisible = false;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
float spawn = 0;
protected override void Update(GameTime gameTime)
{
spawn += (float)gameTime.ElapsedGameTime.TotalSeconds;
foreach(Eggs eggList in eggList)
eggList.Update(graphics.GraphicsDevice);
LoadEggs();
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
Exit();
base.Update(gameTime);
}
public void LoadEggs()
{
if(spawn >= 1)
{
spawn = 0;
if(eggList.Count() < 4)
eggList.Add(new Eggs(Content.Load<Texture2D>("Images/egg"), new Vector2(50, 0)));
}
for(int i = 0; i < eggList.Count; i++)
if(!eggList[i].isVisible)
{
eggList.RemoveAt(i);
i--;
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.LightYellow);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
foreach(Eggs eggList in eggList)
{
Eggs.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
为什么会出现这个错误?
error CS0120: An object reference is required for the non-static field, method, or property 'Game1.E ggs.Draw(SpriteBatch)'
似乎从末尾开始的第 6 行出现问题。
Eggs.Draw(SpriteBatch)
不能这么叫。由于 Eggs 不是静态的 class,Draw 也不是静态方法,这意味着您需要 Eggs 类型的对象来调用方法 Draw。
所以需要这样的东西:
var egg = new Eggs();
egg.Draw(SpriteBatch);
此外,foreach 循环没有意义,不要对项目使用相同的名称,因为它是您正在循环的集合的名称。
绘制中的 foreach 循环:
foreach(Eggs eggList in eggList)
{
Eggs.Draw(spriteBatch);
}
应改为:
foreach(Eggs egg in eggList)
{
egg.Draw(spriteBatch);
}
正如 Luka 所说,它目前直接从 class 调用,通常只适用于静态 classes 和方法。
我只是认为没有必要声明一个新变量,因为您想遍历列表中的鸡蛋,而不是创建一个空 class。所以现在它从 foreach 中的 egglist
声明一个 egg
,并使用那个 egg
调用内部的 draw 函数。