Home > OS >  C# XNA An object reference is required for the non-static field, method, or property
C# XNA An object reference is required for the non-static field, method, or property

Time:04-10

I'm trying to create egg spawner but this error comes out.
Tried to fix this error but unlucky I can't.
I know that XNA Framework is outdated but I use it for learning.

Would someone help me?
Thanks.

Code:

    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);
        }
    }

Why is this error occurring?

error CS0120: An object reference is required for the non-static field, method, or property 'Game1.E ggs.Draw(SpriteBatch)'

CodePudding user response:

Seems that 6. line from the end is making problems. Eggs.Draw(SpriteBatch) can not be called like that. As Eggs is not static class nor Draw is static method means that you need object of type Eggs to call method Draw.

So something like this is needed:

var egg = new Eggs();
egg.Draw(SpriteBatch);

Also, that foreach loop doesn't make sense, don't use same name for item as it is the name of collection that you are looping through.

  • Related