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);
}
Question
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.
Solution 3
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);
}
OTHER TIPS
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.
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);
}
}