Finding the right index of sprite in list

Started by
-1 comments, last by WaZz86 12 years ago
Hello All,
I am having trouble figuring out which car my character is trying to get into. These cars are in a List of sprite. I've used a for statement to add rectangles over all the cars and count a variable to get the right amount of cars in the index.
ill post an example below but i dont like it this way because im just adding rectangles over existing ones and im not really controlling the car im in, as of right now i am only able to have my player turn invisable at these cars. when it comes to writing the code for the movement i need to know which car he is in out of the list so the car may be controllable.


public void Players_selected_Car(GameTime gameTime,Players player, Cars pCars, CarManager carManager)
{
Rectangle playerRec = new Rectangle((int)player.myPlayer.Position.X, (int)player.myPlayer.Position.Y, 50,65);
for(int p = 0; p < carManager.parked_Cars.Count; p++)
{
Rectangle parked_Cars86 = new Rectangle((int)carManager.parked_Cars

.myCar.Location.X, (int)carManager.parked_Cars

.myCar.Location.Y, 50, 70);

if(playerRec.Intersects(parked_Cars86))
{
currentState = Keyboard.GetState();
if (currentState.IsKeyDown(Keys.E) && !previousState.IsKeyDown(Keys.E))
{
player.PlayersDriving = !player.PlayersDriving;
}
previousState = currentState;
}
}
}



as you can tell it is just drawing over each car in the list and i am intersecting with those rectangles.

Does anyone know how i could find the index of the car my player is intersecting with? and what do you think is better a list of sprites or an array of sprites?

Thank you to all who help

This topic is closed to new replies.

Advertisement