简体   繁体   English

在XNA中,如何从另一个类修改主游戏类中的某些内容?

[英]In XNA, how do I modify something in the main game class from another class?

Okay, by default, the Game class for an XNA PC game has a public bool attribute called IsMouseVisible , which when set enables/disables the mouse being visible. 好的,默认情况下,XNA PC游戏的Game类具有一个名为IsMouseVisible的公共bool属性,该属性在设置时启用/禁用鼠标可见。 I have another class called Configs which monitors specific keys being pressed. 我还有一个名为Configs类,它监视被按下的特定键。 Now what I want to do is set it so that when I press a specific key, it will change the value of IsMouseVisible . 现在,我要设置它,以便当我按下特定键时,它将更改IsMouseVisible的值。 This is where I get stuck because I don't know how to change the value from another class. 这是我卡住的地方,因为我不知道如何从另一个类更改值。

I know that I could just create a bool value in the Configs class and make it so that when the key is pressed, the value will change, then make it so that when the main Game class updates, it sets the value for IsMouseVisible to the Configs class' value, but the thing is, I might want to create multiple different values (not just IsMouseVisible ) that can be set in the main Game class from another class. 我知道我可以在Configs类中创建一个bool值,然后按一下该键,该值将更改,然后在主Game类更新时,将IsMouseVisible的值设置为Configs类的值,但事实是,我可能想创建多个不同的值(不仅仅是IsMouseVisible ),可以在另一个类的主Game类中设置这些值。

And there's my question, how could I set something in the main Game class from another class? 我的问题是,我该如何在另一个主要课程的Game类别中设置一些内容? (Without having to create multiple updates for each and every value I want to be accessible.) (无需为我希望可访问的每个值创建多个更新。)

Create the object 'Configs' in the main class's constructor. 在主类的构造函数中创建对象“ Configs”。 While doing this, give the Config objects a reference to the main class. 在执行此操作时,请为Config对象提供对主类的引用。 Now Configs can access the main class's value. 现在,Configs可以访问主类的值。

You will need to pass a reference of the game class to the Configs class. 您将需要将游戏类的引用传递给Configs类。 In the Configs' class constructor, set a private member variable to the instance of the game class. 在Configs的类构造函数中,将私有成员变量设置为游戏类的实例。 From there you can manipulate the game. 从那里您可以操纵游戏。 See below: 见下文:

Game Class 游戏类

public class Game1 : Game
{
    Configs configs; 

    GraphicsDeviceManager _graphics;
    SpriteBatch _spriteBatch;

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        // Pass THIS game instance to the Configs class constructor.
        configs = new Configs(this);
        base.Initialize();
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        base.Draw(gameTime);
    }
}

Configs Class 配置类

public class Configs
{
    // Reference to the main game class.
    private Game _game;

    public Configs(Game game)
    {
       // Store the game reference to a local variable.
        _game = game;
    }

    public void SetMouseVisible()
    {
        // This is a reference to you main game class that was 
        // passed to the Configs class constructor.
        _game.IsMouseVisible = true;
    }
}

This is basically what others have been stating. 这基本上是其他人一直在说的。 However, it seems you are having a rough time understanding the concepts without seeing some code. 但是,似乎您在不了解某些代码的情况下很难理解这些概念。 Therefore, I provided it. 因此,我提供了它。

Hope that helps. 希望能有所帮助。

Three things, and they all relate to your comments on the answer from @Patashu: 三件事,它们都与您对@Patashu的答案的评论有关:

First, if you are passing a Game object into your constructor, you don't need to set it as a ref type argument, because it is automatically passed by reference (by virtue of being an object). 首先,如果要将Game对象传递到构造函数中,则无需将其设置为ref类型参数,因为它是通过引用自动传递的(因为它是对象)。

Second, the reason your GameComponent subclass needs a reference to the Game object in its default constructor...is that it already has a reference to Game built into it. 其次,您的GameComponent子类需要在其默认构造函数中引用Game对象的原因……是因为它已经内置了对Game的引用。 In your Configs class, call this.Game That is the main Game instance. 在您的Configs类中,调用this.Game这是主要的Game实例。

Third, If your constructor already recieves one argument of an object, it does not need another argument of the same instance of the same object. 第三,如果构造函数已经接收到一个对象的一个​​参数,则它不需要相同对象的同一实例的另一个参数。

Read the documentation. 阅读文档。 It's useful. 很有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM