移动列表中的投影

本文关键字:投影 列表 移动 | 更新日期: 2023-09-27 17:58:40

我有一个会发射炮弹的物体,我试图使用一个列表在火箭(炮弹)中生成,这样当它们与物体碰撞时我就可以删除它们。因此,我首先创建了List<Rectangle> Rockets;,然后添加了一个函数,用于不断产生和发射火箭:

        if (Time > 0.2)
        {
            Time = 0;
            Rockets.Add(new Rectangle((int)Position.X, (int)Position.Y, rocketTexture.Width, rocketTexture.Height));
        }

然后我尝试更新它们,这样它们就可以通过做foreach在屏幕上移动:

        foreach (Rectangle r in Rockets)
        {
        }

问题

这就是我陷入困境的地方,我如何调用每个火箭列表中的x和y值,以便在屏幕上移动它?

我可能想得太多了,有一种更容易的方法可以制造大量的投射物,并在与道路碰撞或走得太远时将其展开。

移动列表中的投影

在游戏开发中,您宁愿使用update()方法实现Rocket类,在该方法中,您可以将Rocket移动一些speed_x和speed_y属性。然后,您将在main run()方法中通过调用Rocket.getRect()(或只是一个.Rct属性,它可以在父类Entity或其他类中实现)来检查冲突。

话虽如此,我可能还不明白这个问题。

这里有一个代码示例,希望它能帮助

class Rocket
{
    int _life;
    public Rocket(Vector2 position, Vector2 direction)
    {
        _position = position;
        _direction = direction;
        //life of rocket. Once it reaches zero the rocket is removed.
        _life = 100;
    }
    Vector2 _position
    public Vector2 Position
    {
        get
        {
            return _position;
        }
    }
    Vector2 _velocity;
    Vector2 Velocity
    {
        get
        {
            return _velocity;
        }
    }
    public int Life
    {
        get
        {
            return _life;
        }
    }
    public void Update(World world)
    {
        _life--;
        _position += _velocity;
        //could check for collisions here, eg:
        foreach (Ship ship in world.Ships)
        {
            if (Intersects(ship))
            {
                //collision!
                //destroy this rocket
                _life = 0;
                //damage the ship the rocket hit
                ship.ApplyDamage(10);
                return;
           }
        }
    }
}
class Game
{
    List<Rocket> _rockets = new List<Rocket>();
    List<Rocket> _deadRockets = new List<Rocket>();
    void Update(GameTime gameTime)
    {
        //.ToArray() is used here because the .net list does not allow items to be removed while iterating over it in a loop. 
        //But we want to remove rockets when they are dead. So .ToArray() means we are looping over the array not the 
        //list, so that means we are free to remove items from the list. basically it's a hack to make things simpler...
        foreach (Rocket rocket in _rockets.ToArray())
        {
            //world is an object that contains references to everything on the map
            //so that the rocket has access to that stuff as part of it's update
            rocket.Update( world );

            //if the rocket has run out of life then remove it from the list
            if (rocket.Life <= 0)
                _rockets.Remove(rocket);
        }
    }
    void FireRocket(Vector2 from, Vector2 direction)
    {
        Rocket newRocket = new Rocket(from, direction);
        _rockets.Add(newRocket);
    }
}

执行for循环而不是foreach循环。我还建议使用数组(并创建一个Rocket类,而不仅仅是Vector2s)。

for (int i = 0; i < Rockets.Count; i++)
{
   Rockets[i] = new Rectangle(Rockets[i].X + 20, Rockets[i].Y + 20);
}