简体   繁体   English

从子类(C#)中将对象添加到列表中

[英]Adding objects to a list from within a subclass (C#)

I'm not understanding how to add objects to a list when I'm putting the code in a class definition. 将代码放在类定义中时,我不了解如何将对象添加到列表中。 I'm working with C#. 我正在使用C#。

I'm making a platform game and have set things up so that every object in the game, whether a player character, an enemy, a moving platform or a stationary object, all inherit the same properties held in my "StaticObject" class (which itself inherits a "Sprite" class), building on top of this depending on their specific function. 我正在制作平台游戏,并进行了设置,以便游戏中的每个对象(无论是玩家角色,敌人,移动的平台还是固定的对象)都继承了“ StaticObject”类(本身会继承“ Sprite”类),并根据其具体功能在此基础上构建。 I want to add all objects, regardless of their type, to a list to help me check for collisions. 我想将所有对象(无论其类型如何)添加到列表中,以帮助我检查碰撞。

For example, one sequence of inheritance is: Sprite > StaticObject > MovingObject > Character > PlayerCharacter 例如,一个继承序列是:Sprite> StaticObject> MovingObject> Character> PlayerCharacter

I created a "GameInfo" class (and made a static instance so that its properties can be accessed from anywhere in the game) and defined my "gameObjects" list in there: 我创建了一个“ GameInfo”类(并创建了一个静态实例,以便可以从游戏中的任何位置访问其属性),并在那里定义了“ gameObjects”列表:

class GameInfo
    {
        public static GameInfo gameInfo { get; private set; }
        public List<StaticObject> gameObjects { get; set; }

        public GameInfo()
        {
            GameInfo.gameInfo = this;
            gameObjects = new List<StaticObject>();
        }
    }

Then in the constructor of my "StaticObject" class (which all game objects inherit) I tried to add the object being constructed to the list in "GameInfo": 然后,在我的“ StaticObject”类的构造函数(所有游戏对象都继承)的构造函数中,我尝试将正在构造的对象添加到“ GameInfo”的列表中:

class StaticObject : Sprite
    {
        public StaticObject(Texture2D texture, Vector2 position, SpriteBatch spriteBatch) : base(texture, position, spriteBatch)
        {
            GameInfo.gameInfo.gameObjects.Add(this);
        }

        public override void Update(GameTime gameTime)
        {
            // Collision code.

            base.Update(gameTime);
        }
    }

This doesn't work and I get the error: 这不起作用,我得到了错误:

System.NullReferenceException: 'Object reference not set to an instance of an object.' System.NullReferenceException:'对象引用未设置为对象的实例。

I can't work out how exactly to add every object to my "gameObjects" list at the moment they are created, without having to explicitly add each one when the main game code creates an object. 我无法确定如何在创建对象时将每个对象准确地添加到我的“ gameObjects”列表中,而不必在主游戏代码创建对象时显式添加每个对象。 I want the addition to happen automatically. 我希望添加自动发生。

I've seen a similar question asked elsewhere but I couldn't get my head around the answer. 我在其他地方也看到过类似的问题,但我无法理解。 Any help on this would be greatly appreciated! 任何帮助,将不胜感激! Thanks! 谢谢! :) :)

Instead of GameInfo.gameInfo = this; 而不是GameInfo.gameInfo = this; in the CTOR do public static GameInfo gameInfo { get; private set; } = new GameInfo(); 在CTOR中执行public static GameInfo gameInfo { get; private set; } = new GameInfo(); public static GameInfo gameInfo { get; private set; } = new GameInfo();

Also, you should make the Constructor private. 另外,您应该将构造函数设为私有。

While this will do for the moment, it will be beneficial for you to dig yourself deeper into the Singleton-Pattern if you are planning on doing more coding. 尽管目前可以这样做,但是如果您打算进行更多的编码,则对您更深入地了解Singleton模式将是有益的。 And of course Design Patterns in general are always worth a read. 当然,总的来说,设计模式总是值得一读。

As to why it didn't work: 至于为什么它工作:

You (tried to) set the static reference to the (singleton-)instance of the class in the constructor. 您(试图)将静态引用设置为构造函数中类的(单例)实例。 But the constructor has not yet been executed when you try to access that static field. 但是,当您尝试访问该静态字段时,构造函数尚未执行。 It never would be executed with that code unless you did so explicitly (which you could since the CTOR is public). 除非您明确地这样做(因为CTOR是公开的,所以您可以这样做),否则它将永远不会与该代码一起执行。

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

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