Jump to content
  • Advertisement
Sign in to follow this  
WaZz86

XNA 4.0 Foreach & GetEnumerator

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font=arial,helvetica,sans-serif]

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.

Share this post


Link to post
Share on other sites
Advertisement
Hi.

I think that you are making a mistake :s. You should not have a class cars, but rather have a class class "car" and then have a List<Car> containing all the game cars.
Also you are using the foreach currently here:

foreach (Cars Cars in parked_Cars)
{
Cars.Draw(spriteBatch);
}


Probably i am reading it wrong. That said, Enumerators only make sense in collections nad by looking at the Cars class, i cannot assert that that class represents a single car or a collection of cars..

Also, you can find acomphreensible Enumerator tutorial here. Hope this helps ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!