簡體   English   中英

C# XNA 非靜態字段、方法或屬性需要 object 引用

[英]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);
        }
    }

為什么會出現此錯誤?

錯誤 CS0120:非靜態字段、方法或屬性“Game1.E ggs.Draw(SpriteBatch)”需要 object 引用

似乎 6. 從最后一行開始出現問題。 Eggs.Draw(SpriteBatch)不能那樣調用。 由於 Eggs 不是 static class,Draw 也不是 static 方法,這意味着您需要 object 類型的 Eggs 才能調用方法 Draw。

所以需要這樣的東西:

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

此外,該 foreach 循環沒有意義,不要對 item 使用相同的名稱,因為它是您正在循環的集合的名稱。

您繪制中的 foreach 循環:

foreach(Eggs eggList in eggList)
{
Eggs.Draw(spriteBatch);
}

應該改為:

foreach(Eggs egg in eggList)
{
    egg.Draw(spriteBatch);
}

正如 Luka 所說,它目前直接從 class 調用,通常只適用於 static 類和方法。

我只是認為聲明一個新變量不是必需的,因為你想遍歷列表中的雞蛋,而不是創建一個空的egglist所以現在它從 foreach 中的雞蛋列表中聲明一個egg ,並使用那個egg來內撥function開獎。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM