Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 递归类列表_C#_Xna_Game Engine - Fatal编程技术网

C# 递归类列表

C# 递归类列表,c#,xna,game-engine,C#,Xna,Game Engine,我目前正在学习C#XNA课程,这就是他们实施子弹的方式。他们说该类将自动遍历列表(bullList)。目前这对我不起作用。有谁能给我一个关于我做错了什么的线索,或者给我举一个这个工作版本的例子吗?提前谢谢 class Bullet { public Vector2 position; public Vector2 velocity; public Texture2D texture; public float speed; public Recta

我目前正在学习C#XNA课程,这就是他们实施子弹的方式。他们说该类将自动遍历列表(bullList)。目前这对我不起作用。有谁能给我一个关于我做错了什么的线索,或者给我举一个这个工作版本的例子吗?提前谢谢

    class Bullet
{
    public Vector2 position;
    public Vector2 velocity;
    public Texture2D texture;
    public float speed;
    public Rectangle rectangle;
    public Vector2 size = new Vector2(16, 16);
    public List<Bullet> bullList = new List<Bullet>();
    public enum Owner
    {
        PLAYER,
        ENEMY
    }

    public Bullet()
    {
        Update();
    }

    public void Update()
    {
        rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
        position += (velocity * speed);

    }

    public void Draw(SpriteBatch spriteBatch, Texture2D a_texture)
    {
        spriteBatch.Draw(a_texture, position, Color.White);
    }

    public void Shoot(Vector2 a_position, Vector2 a_velocity, float a_speed, Texture2D a_texture)
    {
        Bullet newBullet = new Bullet();
        newBullet.position = a_position;
        newBullet.velocity = a_velocity;
        newBullet.speed = a_speed;
        newBullet.texture = a_texture;
        newBullet.isAlive = true;
        bullList.Add(newBullet);
    }
}
类项目符号
{
公共向量2位置;
公共矢量2速度;
公共纹理2D纹理;
公众浮标速度;
公共矩形;
公共向量2大小=新向量2(16,16);
public List bullList=新列表();
公共枚举所有者
{
玩家,
敌人
}
公众子弹()
{
更新();
}
公共无效更新()
{
矩形=新矩形((int)position.X,(int)position.Y,(int)size.X,(int)size.Y);
位置+=(速度*速度);
}
公共空白绘制(SpriteBatch SpriteBatch,纹理2D a_纹理)
{
绘制(a_纹理、位置、颜色、白色);
}
公共空射(矢量2 a_位置、矢量2 a_速度、浮动a_速度、纹理2D a_纹理)
{
Bullet newBullet=新Bullet();
newBullet.position=a_位置;
newBullet.velocity=a_速度;
newBullet.speed=a_速度;
newBullet.texture=a_纹理;
newBullet.isAlive=true;
牛头列表。添加(纽布利特);
}
}

绘图和更新功能都在处理bullet的“主”实例。相反,您需要运行foreach:

foreach (Bullet b in bullList)
{
   b.rectangle = new Rectangle((int)b.position.X, (int)b.position.Y, (int)b.size.X, (int)b.size.Y);
   b.position += (b.velocity * b.speed);
}
还有一个类似的用于绘制循环

尽管如此,我不会这样做。子弹没有子弹,BulletManager可能会这样做。换句话说,主“项目符号”有一个包含所有“实际”项目符号的列表,并且没有使用所有位置变量,“实际”项目符号有一个不需要的子项目符号列表,但是它们使用所有位置变量。对我来说,两个不同的对象似乎是一个很好的例子:)


我还注意到,从未使用过“Owner”枚举,“IsAlive”属性。这可能会在以后添加,但在测试中“激发”足够多之后,缺少“IsAlive”可能会导致性能问题。

非常感谢您的回复。所以据我所知,这个类不会“自动”遍历bullList?@user3413042,不,没有任何魔法可以让它发生。如果你想在一个集合上列举一些东西,你必须告诉它这样做。顺便说一句,我们应该很高兴这样做。如果我们包含的集合突然开始在给定函数中枚举,那将是多么糟糕:)非常感谢!删除未使用的项目符号非常有帮助:
BulletList.removeAll(function(c)c.IsAlive=false)