【问题标题】:Add object that called method to a list将调用方法的对象添加到列表中
【发布时间】:2013-10-14 13:28:01
【问题描述】:

我正在尝试用 C# + XNA 制作一个简单的成就类,我想实现一个队列,以便当你快速获得多个成就时,后者将被添加到队列列表中。

这是为获得特定成就而调用的方法:

//This is the list that I want to add the achievements to be queued to
static List<Achievement> queueList;

public void GetAchievement()
{
        //If the player hasn't got the achievement yet and another achievement is NOT being drawn, get the achievement
        if (!got && !drawing)
            get = true;
        //If the player hasn't got the achievement yet and another achievement IS being drawn, add the achievement to the queue list
        else if (!got && drawing)
            queueList.Add(); //What do I do here?
 }

所以这将被称为:exampleAchievement.GetAchievement();其中 exampleAchievement 是 Achievement 类的对象。

我只是不知道如何知道哪个对象正在调用 GetAchievement(),所以我不知道要向 queueList 添加什么。

任何帮助将不胜感激!

~卢卡

编辑: 我用过:

queueList.Add(this);

但我只是愚蠢并且没有正确测试它,所以看起来没有任何东西被添加到列表中。 无论如何感谢您的帮助:3

【问题讨论】:

  • 为什么不总是将成就添加到队列中,即使它只有一个?

标签: c# list queue


【解决方案1】:

您在寻找exampleAchievement 实例吗?

queueList.Add(this);// is this you're looking for?

我错过了什么吗?

【讨论】:

    【解决方案2】:

    听起来你在要求this,它指的是调用实例方法的当前对象。

    【讨论】:

      【解决方案3】:

      如果ExampleAchievement 是从它的基类Achievement 获取这个方法,那么就这样做:

      queueList.Add(this);
      

      并且ExampleAchievement 的实例将被添加到队列中。但是,在使用它时,您需要以某种方式检查类型是否存在您需要获取的基本合同中不可见的特殊属性

      如果所有属性都只是覆盖子类中的属性,那么您无需担心这一点,因为您将在访问它们时取回子实现。

      【讨论】:

        【解决方案4】:

        是否可以选择传递一个参数,该参数可以将自己标识为调用它的对象?

        所以

        public void GetAchievement(string caller)
        {
        
        if(caller == "Something")
        {
        //Do Something
        }
        else
        {
        //Do Something else
        }
             //If the player hasn't got the achievement yet and another achievement is NOT being drawn, get the achievement
            if (!got && !drawing)
                get = true;
            //If the player hasn't got the achievement yet and another achievement IS being drawn, add the achievement to the queue list
            else if (!got && drawing)
                queueList.Add(); //What do I do here?
        }
        

        然后使用 exampleAchievement.GetAchievement(this.name); 调用它

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-12
          • 2018-07-23
          • 1970-01-01
          • 2021-11-29
          • 2019-10-13
          • 2019-05-08
          • 2021-11-24
          • 1970-01-01
          相关资源
          最近更新 更多