如何从另一个创建的类访问 spriteBatch.Draw

本文关键字:访问 spriteBatch Draw 创建 另一个 | 更新日期: 2023-09-27 18:32:53

我正在尝试从我创建的类中访问 draw 方法,以根据随机生成的数字绘制精灵。当我尝试访问这些时,我得到的错误是这样的:

错误 1 无法访问外部类型的非静态成员

public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            Texture2D EarthGrass;
            Texture2D EarthDirt;
            Texture2D PooperMachoOre;
            Vector2 BlockBrush;
            System.Random IDB = new System.Random(1);
            System.Random IDB2 = new System.Random(5);
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }
            public class WorldGenerationOverall
            {
                System.Random IDB = new System.Random();
                Vector2 BlockBrush = new Vector2(0, 300);

                public WorldGenerationOverall()
                {
                    int constructer = 0;
                    constructer += 1;
                }
                public void Generate0H()
                {
                    BlockBrush.X = 300;
                }
                public void GenerateHeight10()
                {             
                    for (BlockBrush.Y = 320; BlockBrush.Y < 500; BlockBrush.Y += 20)
                    {
                        for (BlockBrush.X = 0; BlockBrush.X < 820; BlockBrush.X += 20)
                        {
                            int IDBint = IDB.Next(11);
                            if (IDBint == 10)
                            {                            
                                spriteBatch.Draw(PooperMachoOre, BlockBrush, Color.White);
                            }
                            else
                            {
                                spriteBatch.Draw(EarthDirt, BlockBrush, Color.White);
                            }
                        }
                    }
                }
            }
            public override void Initialize()
            {
                // TODO: Add your initialization logic here            
                BlockBrush = new Vector2(0, 300);
                WorldGenerationOverall super = new WorldGenerationOverall();
                base.Initialize();
            }
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                WorldGenerationOverall super = new WorldGenerationOverall();

                // TODO: use this.Content to load your game content here
                EarthDirt = Content.Load<Texture2D>("Sprites/Ground/EarthDirt");
                EarthGrass = Content.Load<Texture2D>("Sprites/Ground/EarthGrass");
                PooperMachoOre = Content.Load<Texture2D>("Sprites/Ores/PooperMachoOre");
            }

            public override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.White);
                // TODO: Add your drawing code here' 
                spriteBatch.Begin();
                WorldGenerationOverall super = new WorldGenerationOverall();
                super.Generate0H();
                super.GenerateHeight10();
                spriteBatch.End();
                base.Draw(gameTime);
            }
        }
    }

如何从另一个创建的类访问 spriteBatch.Draw

您可以简单地通过方法中的参数传递SpriteBatch,如下所示:

public void GenerateHeight10(SpriteBatch spriteBatch)
{
      //Do stuff with spriteBatch
}

WorldGenerationOverall super = new WorldGenerationOverall();
super.Generate0H();
super.GenerateHeight10(spriteBatch);

另外,只是好奇,为什么要每帧制作一个新的WorldGenerationOverall对象?