# XNA Firing and Storing Values in Lists

This topic is 3392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi again. i am a little confused with adding items to Lists. I am writing a 2D Fortress game where the user controls a turret and fires bullets at incoming Sprites. at the moment I am trying to write the Game logic for bullets, here is an outline of what I want to do. 1/ Create a new bullet at the location of the barrel of the gun 2/ Set the Bullet shooting off in the direction the barrel is facing(i know the rotation) seemed easier than it was for me; here is what i am trying:
if(enough time has passed since you last fired)
{
create a new Bullet at the position of the barrel
get the rotation of the barrel and transform a vector to the currant rotation
set the bullet moving in the direction at a given speed.
!!** Add this bullet to the BulletList **!! //This is what I'm confused with
}
struct Bullet
{
public Vector2 Pos;
public Vector2 Dir;
public void Update()
{
Pos = new Vector2(Pos.X + Dir.X, Pos.Y + Dir.Y);
}
}

Bullet[] bullet;
List<Bullet[]> Bulletlist = new List<Bullet[]>();
private void FireBullet()
{
if (timeScinceFire > FireDelay)
{
bullet[BulletList.Count + 1].Position = //some code to get the position
bullet[BulletList.Count + 1].Direction= //Some code to get the vect & transform
}
}
private void DrawBullets()
{
foreach(Bullet bullet in Bulletlist)
{
bullet.Update();
spriteBatch.Draw(tBullet, bullet.Pos, Color.White)
}
}

However this will not work as it think it should. Problems: 1/ Bullet does not Update() and move forward; 2/ first bullet draws at (1,1) ALWAYS Can you guys see any problems?
bullet[BulletList.Count + 1]
I don't like this bit. Im not sure why I just don't think it looks right

##### Share on other sites
Again, from the last topic, why are you using arrays here?

Bullet[] bullet;List<Bullet[]> Bulletlist = new List<Bullet[]>();

Why not just a list of Bullets?

List<Bullet> Bulletlist = new List<Bullet>();

Your FireBullets method would then be, for example:

private void FireBullet(){               if (timeScinceFire > FireDelay)   {         Bullet bullet;         bullet.Position = //some code to get the position          bullet.Direction= //Some code to get the vect & transform         BulletList.Add(bullet);   }}

This isn't so much an XNA question as it is a general C# question around the usage of lists.

##### Share on other sites
Quote:
 Original post by Andy474 However this will not work as it think it should. Problems:1/ Bullet does not Update() and move forward;2/ first bullet draws at (1,1) ALWAYS Can you guys see any problems?

1) I would suspect this is because you have a List of Array items (kind of a monster of a multi-dimensional list). When you do the following:

foreach(Bullet bullet in Bulletlist)    {         bullet.Update();...

It looks like it's calling .Update() on the array, not on each array item. I think you would need another loop going through the items of that, bah! Just use a single list like adt7 mentions. List<Bullet>.

2) Unsure of this but it would be somewhere in this code:

         bullet[BulletList.Count + 1].Position = //some code to get the position          bullet[BulletList.Count + 1].Direction= //Some code to get the vect & transform

You would need to give us the code for setting the position but all you should need to do is set Bullet.Position = new Vector2<x, y> to set it somewhere. I would also suggest making sure to use normalized amounts otherwise you'll get some wonky speeds at different angles.

##### Share on other sites
Quote:
 Original post by adt7This isn't so much an XNA question as it is a general C# question around the usage of lists.

thanks :) I usually program in C++ i only started using C# a few weeks ago so I am a bit of a 'noob' you might say with Lists. I am a little sketchy with the .NET framework;

i removed the List of arrays as you suggested. now I have the problem that my bullet still wont Update(); properly as the vector is always the same

struct Bullet{     public Vector2 Pos;     public Vector2 Dir;     public void Update()     {          Pos = new Vector2(Pos.X + Dir.X, Pos.Y + Dir.Y);     }}private void FireBullet(){               if (timeScinceFire > FireDelay)    {        Bullet bullet;        bullet.Pos = new Vector2(myTurret.PosVect.X - 88, myTurret.PosVect.Y);        bullet.Dir = new Vector2(-2, 0);        myTurret.Ammo--;        Bulletlist.Add(bullet);                 }}

is this because of my code in Bullet::Update() or the way i am firing the bullets in FireBullet()?

##### Share on other sites
I can't see where the problem lies, although I'd personally write your update method as:

this.Pos += this.Dir;

The this keywords are probably redundant, they just help my thought process.

You'd be better off wraping up your Pos and Dir in properties, but it's probably a better to get it working before you think about that.

I wish I could help more, but I'm separated from my dev tools for the weekend so I'm unable to quickly code up something to test this.

##### Share on other sites
thanks for that :) looks much neater
this.Pos += this.Dir

i cannot see why this isn't working I have run it and added a breakpoint at this point of code and it executes. so the only conclusion I can see if that this.Dir is a value of (0,0) for some reason.

EDIT :: Although i wonder why i cant do this

private void DrawBullets(){      foreach(Bullet bullet in Bulletlist)      {           bullet.Pos += new Vector2(-2, 0); //Error           spriteBatch.Draw(tBullet, bullet.Pos, Color.White);      }}

Error 1 Cannot modify members of 'bullet' because it is a 'foreach iteration variable'

##### Share on other sites
If you've got a breakpoint on it in VS you should be able to hover over Dir and see it's value, same with Pos.

##### Share on other sites
aah thanks, ok so both pos, and Dir have the correct values. although i notice that bullet.Pos ALWAYS has the same value thats'd be my problem

##### Share on other sites
Try switching bullet to a class instead of a struct. Not ideal, but structs don't play nice with foreach loops and different solutions are needed.

Edit:

If that works, switch it back to a struct and try the following:

for (int i = 0; i < bulletList.Count; i++) {     Bullet bullet = bulletList;     bullet.Update();       spriteBatch.Draw(tBullet, bullet.Pos, Color.White);       bulletList = bullet;}

That is just off the top of my head, hopefully it will compile and run.

##### Share on other sites
public class Bullet{     public Vector2 Pos;     public Vector2 Dir;     public void Update()     {          this.Pos += this.Dir;     }}

I changed it to a class this has fixed the issue. I was just playing on my xBox and it occured to me you can edit member varibles in a foreach iteration now it works just great thanks a lot atd7

1. 1
2. 2
frob
20
3. 3
Rutin
17
4. 4
5. 5

• 13
• 10
• 9
• 18
• 9
• ### Forum Statistics

• Total Topics
632555
• Total Posts
3007035

×