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

时间:2013-09-25 23:49:41

标签: c# xna

我试图从我创建的类中访问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);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

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

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

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

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