Jump to content
  • Advertisement
Sign in to follow this  

Finding the right index of sprite in list

This topic is 2408 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

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);

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

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!