Hello All,[/font]
I am having trouble keeping track of which sprite i want to control in game for eaxmple getting into a car sprite that is in a list of sprites and controlling it. i tried using a for statement but it didnt work the way i like ive been told to look into a foreach statement for this.
///////////////////////////// CollisionDetection.cs //////////////////////////////////////
public void Players_selected_Car(GameTime gameTime, Players player, Cars pCars, CarManager carManager)
{
foreach (Cars Cars in pCars)
{
currentState = Keyboard.GetState();
if (player.myPlayer.BoundingBoxRec.Intersects(pCars.myCar.BoundingBoxRec)
&& currentState.IsKeyDown(Keys.E) && !previousState.IsKeyDown(Keys.E))
{
player.PlayersDriving = !player.PlayersDriving;
}
previousState = currentState;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// CarManager.cs /////////////////////////////
public void LoadContent(ContentManager Content)
{
pcarTexture = Content.Load<Texture2D>("Cars Spritesheet");
this.initialFrame = new Rectangle(0, 0, 54, 70);
this.frameCount = 0;
pcarTexture.Bounds.Equals(new Rectangle(0, 0, 39, 50));
}
public void Initialize()
{
parked_Cars = new List<Cars>();
setup_ParkedCars();
}
public void SpawnCars(int space)
{
Cars doublestriped_GreenCar = new Cars(pcarTexture,
new Vector2(700, 270),
new Rectangle(0, 0, 54, 70),
0);
Cars doublestriped_BlueCar = new Cars(pcarTexture,
new Vector2(700, 320),
new Rectangle(0, 78, 54, 70),
0);
Cars Black_SUV = new Cars(pcarTexture,
new Vector2(700, 390),
new Rectangle(29, 196, 35, 63),
0);
Cars Tan_SUV = new Cars(pcarTexture,
new Vector2(700, 460),
new Rectangle(29, 299, 35, 63),
0);
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (Cars Cars in parked_Cars)
{
Cars.Draw(spriteBatch);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// Cars.cs /////////////////////////////////
public IEnumerator<Cars> GetEnumerator(); // Dont know how to use this properly
public Cars(Texture2D texture,
Vector2 location,
Rectangle initialFrame,
int frameCount)
{
myCar = new AnimatedSprites(location,
texture,
initialFrame,
Vector2.Zero);
previousLocation = location;
currentWaypoint = location;
}
There is much more in those classes but i felt there was no need to post it for what i need help in.
i dont know how to use GetEnumerator for my list of sprites
Thanks to all those who help.
public void Players_selected_Car(GameTime gameTime, Players player, Cars pCars, CarManager carManager)
{
foreach (Cars Cars in pCars)
{
currentState = Keyboard.GetState();
if (player.myPlayer.BoundingBoxRec.Intersects(pCars.myCar.BoundingBoxRec)
&& currentState.IsKeyDown(Keys.E) && !previousState.IsKeyDown(Keys.E))
{
player.PlayersDriving = !player.PlayersDriving;
}
previousState = currentState;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// CarManager.cs /////////////////////////////
public void LoadContent(ContentManager Content)
{
pcarTexture = Content.Load<Texture2D>("Cars Spritesheet");
this.initialFrame = new Rectangle(0, 0, 54, 70);
this.frameCount = 0;
pcarTexture.Bounds.Equals(new Rectangle(0, 0, 39, 50));
}
public void Initialize()
{
parked_Cars = new List<Cars>();
setup_ParkedCars();
}
public void SpawnCars(int space)
{
Cars doublestriped_GreenCar = new Cars(pcarTexture,
new Vector2(700, 270),
new Rectangle(0, 0, 54, 70),
0);
Cars doublestriped_BlueCar = new Cars(pcarTexture,
new Vector2(700, 320),
new Rectangle(0, 78, 54, 70),
0);
Cars Black_SUV = new Cars(pcarTexture,
new Vector2(700, 390),
new Rectangle(29, 196, 35, 63),
0);
Cars Tan_SUV = new Cars(pcarTexture,
new Vector2(700, 460),
new Rectangle(29, 299, 35, 63),
0);
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (Cars Cars in parked_Cars)
{
Cars.Draw(spriteBatch);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// Cars.cs /////////////////////////////////
public IEnumerator<Cars> GetEnumerator(); // Dont know how to use this properly
public Cars(Texture2D texture,
Vector2 location,
Rectangle initialFrame,
int frameCount)
{
myCar = new AnimatedSprites(location,
texture,
initialFrame,
Vector2.Zero);
previousLocation = location;
currentWaypoint = location;
}