【问题标题】:How to pass references to a class in C#/XNA/Monogame如何在 C#/XNA/Monogame 中传递对类的引用
【发布时间】:2013-12-19 13:02:16
【问题描述】:

我正在 XNA 中制作一个 2D 塔防游戏,对于我的敌人,我需要将他们的起始坐标传递给 Game1 类,以便我可以在正确的位置绘制它们。但是,由于 Game1 是一个预制类,我认为我不能为它们传递参考。有没有办法通过引用传递变量而不修改类的参数(我认为这是括号中的内容)。 只是要求澄清。谢谢你!!! (Ant 是敌人,startplace 定义在 ant 的基类中)

P.S BUg.startplace 不起作用

代码:

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            Content.RootDirectory = "Content";
            background = Content.Load<Texture2D>("background");

            Texture2D AntTexture = Content.Load<Texture2D>("ant");
//ant1 is already made I'm just setting it to something here
            ant1 = new Ant(AntTexture, Bug.startplace, 100, 10, 0.5f);
            Texture2D BlueberryBushTexture = Content.Load<Texture2D>("blueberrybush");
            player = new Player(level, BlueberryBushTexture);




            // TODO: use this.Content to load your game content here
        }

【问题讨论】:

  • 您确定在Game1 类中创建这些对象吗?在渲染管道开始之前,您的游戏不会绘制任何内容..
  • 是的,我正在 Game1 中创建类,但是为了在正确的位置绘制它(只是开始),我需要传入类中确定的坐标。
  • 它们是你的敌人类的公共属性吗?
  • 是的,它是公开的,我应该把它放在构造函数中吗?
  • 显示一些代码并解释你想用它做什么。我可以猜几个小时你的代码是什么样子的。

标签: c# xna monogame


【解决方案1】:

所以我意识到,startplace 变量实际上并不重要,因为绘图是稍后完成的。我混合了 draw() 和 LoadContent() 方法。对不起这个脑残!!!

【讨论】: