Visual C# Picturebox Objects
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?
Answer by ThomasVandenberghe · Mar 18, 2016 at 03:09 PM
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
}
}
Your answer
Follow this Question
Related Questions
On collision enter next scene 2 Answers
How to destroy Terrain-Tree-Collider? 3 Answers
Problem with collider 2 Answers
C# OnTriggerEnter Issue 3 Answers
OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers