0

I have an object that will fire out projectiles, I was trying to use a list to spawn in the Rockets(projectiles) so I could delete them when they collide with an object. So I first create List<Rectangle> Rockets; I then add in a function for the rockets to be spawned in and fired constantly:

        if (Time > 0.2)
        {
            Time = 0;

            Rockets.Add(new Rectangle((int)Position.X, (int)Position.Y, rocketTexture.Width, rocketTexture.Height));
        }

I then try to update them so they will move across the screen by doing a foreach:

        foreach (Rectangle r in Rockets)
        {
        }

Question

This is where I get stuck, how do I call upon the x and y value inside the list of each Rocket so i can move it across the screen?

I may be thinking too hard about this and there is an easier way to create a large amount of projectiles and have them despawn when colliding with a way or when they go too far.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Ryan Foy
  • 322
  • 6
  • 19

3 Answers3

3

In game development, you'd rather implement a Rocket class with a update() method, in which you'd move the rocket by some speed_x and speed_y attribute. You would then in your main run() method check collisions by calling Rocket.getRect() (or just a .Rect property, which could be implemented in a parent class Entity or something).

That being said, i may not have understood the problem.

Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • The asker is trying to modify these values, but it's impossible using a `foreach` loop. – joppiesaus Apr 16 '14 at 14:02
  • Well, as far as I remember, Rectangle has X and Y as public fields, no trouble modifying those in a foreach. – Kilazur Apr 16 '14 at 14:10
  • I think what Kilazur is correctly saying is that a rocket should be encapsulated in a class. The rocket class should expose an Update method which is called from the main game loop and inside that update method the Rocket updates it's position internally. – Weyland Yutani Apr 16 '14 at 15:04
1

Here's a code example, hope it helps

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);
    }
}
Weyland Yutani
  • 4,682
  • 1
  • 22
  • 28
0

do a for loop instead of a foreach loop. I also recommend using arrays(and make a Rocket class instead of just Vector2s).

for (int i = 0; i < Rockets.Count; i++)
{
   Rockets[i] = new Rectangle(Rockets[i].X + 20, Rockets[i].Y + 20);
}
joppiesaus
  • 5,471
  • 3
  • 26
  • 36
  • Why should I use an array? what advantages would that bring? [Here a list a better thing vs an array.](http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which) – Ryan Foy Apr 16 '14 at 14:29
  • Don't use an array you are right to be using a list. – Weyland Yutani Apr 16 '14 at 15:08
  • @RyanFoy I _recommend_ arrays. I prefer them because modifying values and stuff is easier. There's nothing wrong with a list. – joppiesaus Apr 17 '14 at 13:59
  • @WeylandYutani Could you please explain why you shouldn't use an array? – joppiesaus Apr 17 '14 at 14:03
  • Any ways thank you for the help I had forgotten that I could do a for loop. I did not know how to witch was my fault for not studying up a little more. Thank you for the code help. – Ryan Foy Apr 18 '14 at 02:17