C# array question

Started by
14 comments, last by ChristianFrantz 11 years ago

Thats not what rip-off was trying to suggest. He was saying if you use an array you have to keep track of whether the sprite should be visible (i.e has been hit or not) in a list when its hit you just remove it from the list and no longer need to check whether it needs to be rendered, updated or have collisions for hits against it etc anymore.

Advertisement

My 2-cents: Array is going to cause more problems that it would solve.

So, you use an array Alien[24] and delete Alien[16] well now your list has NULL. you now have to check for this or risk having an exception thrown, if you use a List<Alien> when you remove element 16, there is no null, just 13 elements in the list.

The only advantage a Array will give you over a list of perhaps with drawing


(for int x; x < 6; x++) 
    DrawAt(x * 64)
 

but again, this can be done using a List Too.

OK so this is what I have so far


int alienCount = 6; 

public List<Vector2> alienPos = new List<Vector2>();

in the start of my class


            for (int x = 0; x < alienPos.Count; x++)
            {
                alienPos[x] = new Vector2 (20*x + 50, 50);
            }

this is my initialize method


for (int x = 0; x < alienPos.Count; x++)
{
alien.Draw(spriteBatch, alienPos[x]);
}

It compiles and runs but the aliens arent being drawn on the screen

Edit: figured out that I wasn't ever adding anything to the list -.- but heres the code for that


alienPos[x].Add(new Vector2(x + 20 + 50, 20));

And it gives me the error that Add doesnt take 1 argument

If you see a post from me, you can safely assume its C# and XNA :)

Hey burnt_casadilla,

Just a few things:

You shouldn't need alienCount or alienPos. Rather, you'll want to have a List<Alien> that stores its Position, so that it can Draw itself with only spriteBatch passed in as a parameter.

The reason for the error, is that you're actually calling Vector2.Add (which adds together 2 Vector2), since you're referring to the item in the List itself by specifying the index.

OK so this is what I have so far


int alienCount = 6; 

public List<Vector2> alienPos = new List<Vector2>();

in the start of my class


            for (int x = 0; x < alienPos.Count; x++)
            {
                alienPos[x] = new Vector2 (20*x + 50, 50);
            }

for (int x = 0; x < alienPos.Count; x++)
{
alien.Draw(spriteBatch, alienPos[x]);
}

It compiles and runs but the aliens arent being drawn on the screen

Edit: figured out that I wasn't ever adding anything to the list -.- but heres the code for that


alienPos[x].Add(new Vector2(x + 20 + 50, 20));

And it gives me the error that Add doesnt take 1 argument

because alienPos is a List<Vector2> and the indexer alienPos[x] returns a Vector2 so you are doing Vector2.Add(..) like ndssia said.

alienpos.Add(new Vector2(x + 20 + 50, 20)); //Why 20 + 50? why not just 70 :P seems a little silly

I actually figured it out at 3 am when my brain was shutting off :P very strange. But thank you everyone for the help!! One last request...I'm going to need a timer or something along those lines. I want the aliens to move a certain x position every second, and then in a time span of 5 seconds I'm going to have them shoot their bullets randomly so not every alien is shooting at once

If you see a post from me, you can safely assume its C# and XNA :)

This topic is closed to new replies.

Advertisement