AlfonsAberg

This is what my code looks like now:

"static public void CreatePlayers(int numberOfPlayers)
{
switch(numberOfPlayers)
{
case 1:
players = new Player[1] { new Player() };
playerSprites = new Animation[1] { new Animation() };
break;
case 2:
players = new Player[2] { new Player(), new Player() };
playerSprites = new Animation[2] { new Animation(), new Animation()};
break;
case 3:
players = new Player[3] { new Player(), new Player(), new Player() };
playerSprites = new Animation[3] { new Animation(), new Animation(), new Animation() };
break;
case 4:
players = new Player[4] { new Player(), new Player(), new Player(), new Player() };
playerSprites = new Animation[4] { new Animation(), new Animation(), new Animation(), new Animation() };
break;
}
}"

What I want is to use a loop and add the objects to the Arrays instead, but I don't know how to do that simple thing.

Cheers
Alfons


Re: XNA Game Studio Express How to Add object to Array?

Ronald Ricardo Ramirez Moran

Use it code instead:

public static void CreatePlayers(int numberOfPlayers)

{

players = new Player[numberOfPlayers];

playerSprites = new Animation[numberOfPlayers];

for (int i = 0; i < numberOfPlayers; i++)

{

players[ i ] = new Player();

playerSprites[ i ] = new Animation();

}

}






Re: XNA Game Studio Express How to Add object to Array?

AlfonsAberg

thanks alot!