# 2D Object 'Targeting' / 'Selecting'

I'm currently working with C# with the XNA framework.

I'm trying to create a list of objects that are individually target-able after they are created and set in a list and displayed on a tile map.

For example, I have a blank tile map and I cycle through the tiles and randomly set objects (we'll say a box) throughout the map. When I set the box object, I add that new iteration of Box to a Box List.

At this point I have a tile map with a series of randomly placed boxes. Within these boxes I also have randomly picked values for their name, size, color etc. And these values are correctly stored within the box object in its respective index in the List.

My question is how do I go about setting up a method for targeting/selecting a box so that when I click on any given box it's respective data values appear? I'm not worried about graphical representation of the selected object but the retrieval of the objects specific data via a mouse click on its position.

I've set up Events to handle the changing of my game states, does this require the same methodology or something all together different?

Thanks for your time.

• I'm not sure I understand the question. So you're saying after you click on a box, how would you display it's data? If you've clicked on a box that implies that you have a reference for that box available and therefore it's data is there for you to display however you want. I'm not even sure what this has to do with polymorphism. – Mike Cluck Mar 13 '12 at 5:41
• I guess saying polymorphism is a bit pointlessly detailed. Sorry. Say I've got a Box class and I have a SmallBox class that inherits from my Box class. Then I create several objects of my SmallBox class, all with different variable values and locations on the map. I'm looking for an effective way to click on it based on its location and have (a DrawString) show that data on screen. My initial thinking was using a Rectangle defined when the box is placed on the map, but I'm unsure of the most concise way to do it. – AndrewAchilles Mar 13 '12 at 6:37

If the boxes have an arbitrary size (meaning that they are not the same size as your tiles) use this method:

// Loop through your list of boxes and check to see if the mouse click coordinates is inside the box.

MouseState ms = Mouse.GetState();
Box selectedBox = null;
foreach(Box b in boxList)
{
if(b.Bounds.Contains(ms.X, ms.Y)
{
//box found
selectedBox = b;
break;
}
}

// display information about the 'selectedBox' if it is not equal to null.

• If this answered your question please mark it as the answer. Thanks! – Luis Estrada Mar 13 '12 at 20:51