I am making a small game where I need to detect collisions with pictureboxes which are obstacles (Walls)
I’m trying to do something like this:
if (Player.Bounds.IntersectWith(Wall.Bounds)) { // do something }
But I dont know how to put a certain amount of pictureboxes into one object, Walls.
So I result in doing something like this
if (Player.Bounds.IntersectWith(Wall1.Bounds || Player.Bounds.IntersectWith(Wall2.Bounds || etc)) { // do something }
Help?
You should try using lists or array, I’d advise using lists here, since it will be easier for you to use.
First of all, put this line of code at the top of your file, it needs to be included in order to use List
using System.Collections.Generic;
Then for the other code
List<PictureBox> walls = new List<PictureBox>(); // Create a new list like this
walls.Add(wall); // Add PictureBox objects
foreach (PictureBox wall in walls)
{
if (Player.Bounds.IntersectWith(wall.Bounds)
{
// do something
break; // This will break the loop, once it has found 1 occurrence which intersects
}
}