检查特定列表索引处的子类

发布于 2024-10-26 17:48:12 字数 659 浏览 6 评论 0原文

您好,我想知道如何检查列表中特定位置的对象是否等于某个对象。

我有 3 个“Ship”子类,它们被称为“Enemy”、“Enemy2”、“Player” 所有这些都保存在我称之为“船舶”的列表中,

我想知道您如何在列表中检查索引处的项目是否属于上述项目之一。这很难解释,我会尝试用代码来解释。

for (int i = 0; i < Game1.Ships.Count; i++)
    {
    if(Game1.Ships.ElementAt(i) == "Enemy")
        Enemy e = Game1.Ships.ElementAt(i);
        if (this.collisionBox.Intersects(e.collisionBox))
        {
            e.Destroy(false);
            //Execute Destory(bool).
        }
    }
    else
        i++;
        //Skip to next item.

这大致就是我想要做的,显然我需要检查它是否不是玩家。我还必须对 Enemy2 执行相同的循环。 请注意,虽然“Ship”默认情况下没有 Destroy(bool),但它只存在于“Enemy”和“Enemy”上。 “敌人2”。

Hi I'm wondering how you would check if an object at a specific location in a List is equal to something.

I have 3 subclasses of "Ship", they are called "Enemy", "Enemy2", "Player"
All of them are saved in a List that I call "Ships"

I'm wondering how you would check in a list that the item at index is of one of the above. It's pretty hard to explain, I'll try to explain in code.

for (int i = 0; i < Game1.Ships.Count; i++)
    {
    if(Game1.Ships.ElementAt(i) == "Enemy")
        Enemy e = Game1.Ships.ElementAt(i);
        if (this.collisionBox.Intersects(e.collisionBox))
        {
            e.Destroy(false);
            //Execute Destory(bool).
        }
    }
    else
        i++;
        //Skip to next item.

That is roughly what I'm trying to do, obviously I'd need to check that It's not a Player. And I'd also have to do the same loop for Enemy2.
Observe though that "Ship" does not have a Destroy(bool) by default, it's only exsists on "Enemy" & "Enemy2".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑梦风尘 2024-11-02 17:48:12

只需使用 is

for (int i = 0; i < Game1.Ships.Count; i++)
{
    if(Game1.Ships.ElementAt(i) is Enemy)
    {
        Enemy e = (Enemy)Game1.Ships.ElementAt(i);
        if (this.collisionBox.Intersects(e.collisionBox))
        {
            e.Destroy(false);
            //Execute Destory(bool).
        }
    }
    else
        i++;
        //Skip to next item.
}

just use is:

for (int i = 0; i < Game1.Ships.Count; i++)
{
    if(Game1.Ships.ElementAt(i) is Enemy)
    {
        Enemy e = (Enemy)Game1.Ships.ElementAt(i);
        if (this.collisionBox.Intersects(e.collisionBox))
        {
            e.Destroy(false);
            //Execute Destory(bool).
        }
    }
    else
        i++;
        //Skip to next item.
}
九歌凝 2024-11-02 17:48:12

您绝对不想像这样使用 ElementAt() ,因为它每次都会遍历序列的开头。我建议使用 foreach 循环和 OfType<>() 来代替:

foreach (var e in Game1.Ships.OfType<Enemy>())
{
    if (this.collisionBox.Intersects(e.collisionBox))
        e.Destroy(false);
}

或者,如果 Game1.Ships 实现 ICollection code>,您可以使用 for 循环并将 .ElementAt(i) 替换为 [i]

for (int i = 0; i < Game1.Ships.Count; i++)
{
    var e = Game1.Ships[i] as Enemy;
    if (e != null)
        if (this.collisionBox.Intersects(e.collisionBox))
            e.Destroy(false);
}

You absolutely do not want to use ElementAt() like this, as it iterates over the start of the sequence every time. I would suggest using a foreach loop and OfType<>() instead:

foreach (var e in Game1.Ships.OfType<Enemy>())
{
    if (this.collisionBox.Intersects(e.collisionBox))
        e.Destroy(false);
}

Or, if Game1.Ships implements ICollection, you could use your for loop and replace .ElementAt(i) with [i]:

for (int i = 0; i < Game1.Ships.Count; i++)
{
    var e = Game1.Ships[i] as Enemy;
    if (e != null)
        if (this.collisionBox.Intersects(e.collisionBox))
            e.Destroy(false);
}
千紇 2024-11-02 17:48:12

并考虑

 foreach (var e in Game.Ships.OfType<IDestroy>())
     e.Destroy()

这似乎是一个不错的策略

“运送”不
默认情况下有一个 Destroy(bool),它是
仅存在于“敌人”和“敌人”中“敌人2”

我不能/不会添加界面以方便选择船舶类型,那么您可以求助于

var destroyables = Game.Ships
           .OfType<Enemy>()
     .Concat(
           Game.Ships.OfType<Enemy2>());

我需要指出哪种方法会获得我的投票吗?

and consider

 foreach (var e in Game.Ships.OfType<IDestroy>())
     e.Destroy()

This would seem to be a nice strategy seeing that

"Ship" does not
have a Destroy(bool) by default, it's
only exsists on "Enemy" & "Enemy2"

I you cannot/will not add the interface to make it easy to select the ship types then you could resort to

var destroyables = Game.Ships
           .OfType<Enemy>()
     .Concat(
           Game.Ships.OfType<Enemy2>());

Do I need to point out which method would have my vote ?

雨后彩虹 2024-11-02 17:48:12

或者 LINQish:

var enemiesToDestroy = from enemy in Game1.Ships.OfType<Enemy>()
                       where this.collisionBox.Intersects(enemy.collisionBox)
                       select enemy;
enemiesToDestroy.ToList().ForEach(enemy => enemy.Destroy(false));

如果您想摆脱 ToList() 转换,请定义以下扩展方法

public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource>   action)
{
    foreach (TSource element in source)
        action (element);
}

并像这样使用它:

enemiesToDestroy.ForEach(enemy => enemy.Destroy(false));

Or LINQish:

var enemiesToDestroy = from enemy in Game1.Ships.OfType<Enemy>()
                       where this.collisionBox.Intersects(enemy.collisionBox)
                       select enemy;
enemiesToDestroy.ToList().ForEach(enemy => enemy.Destroy(false));

If you want to get rid of the ToList() conversion, define the following extension method

public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource>   action)
{
    foreach (TSource element in source)
        action (element);
}

And use it like this:

enemiesToDestroy.ForEach(enemy => enemy.Destroy(false));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文