Question

Is there a way to group 30+ labels to be able to control them all at once. What I want to do is this with 30 labels.

if (player.Bounds.IntersectsWith(label1.Bounds))
        {
            if (right == true)
            {
                right = false;
                left = true;
            }
            else if (left == true)
            {
                left = false;
                right = true;
            }
            else if (up == true)
            {
                up = false;
                down = true;
            }
            else if (down == true)
            {
                down = false;
                up = true;
            }

And then where the label1 is checking if it is colided I want it to check all 30 labels if they have colided. And preferably not with 30x this code and just change the number. =)

I just want to add this is a maze game and the left, right etc. is the players movement defined outside what I posted here. I hope you understand!

All of my code:

    namespace mazeGame
    {
public partial class Form1 : Form
{
    bool down;
    bool left;
    bool right;
    bool up;
 //   new List<int> blocks = new List[5];

    public Form1()
    {
        InitializeComponent();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        if (label1.Bounds.IntersectsWith(label10.Bounds))
        {

        }
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right)
        {
            down = false;
            up = false;
            right = true;
            left = false;
        }
        if (e.KeyCode == Keys.Left)
        {
            left = true;
            down = false;
            up = false;
            right = false;
        }
        if (e.KeyCode == Keys.Up)
        {
            up = true;
            down = false;
            right = false;
            left = false;

        }
        if (e.KeyCode == Keys.Down)
        {
            down = true;
            up = false;
            right = false;
            left = false;
        }
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right)
        {
            right = true;
            left = false;
            up = false;
            down = false;
        }
        if (e.KeyCode == Keys.Left)
        {
            left = true;
            right = false;
            up = false;
            down = false;
        }
        if (e.KeyCode == Keys.Up)
        {
            up = true;
            left = false;
            right = false;
            down = false;
        }
        if (e.KeyCode == Keys.Down)
        {
            down = true;
            left = false;
            up = false;
            right = false;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (player.Bounds.IntersectsWith(label1.Bounds))
        {
            if (right == true)
            {
                right = false;
                left = true;
            }
            else if (left == true)
            {
                left = false;
                right = true;
            }
            else if (up == true)
            {
                up = false;
                down = true;
            }
            else if (down == true)
            {
                down = false;
                up = true;
            }
        }

        var labels = this.??????? // here is where i need help.

        if (right == true)
        {
            player.Left += 1;
        }
        if (left == true)
        {
            player.Left -= 1;
        }
        if (up == true)
        {
            player.Top -= 1;
        }
        if (down == true)
        {
            player.Top += 1;
        }
    }
}
Was it helpful?

Solution

OfType will get all controls of the same type from a control. In this case it will be labels from your form:

var labels = this.myForm.Controls.OfType<Label>()

Then you can iterate through your collection of labels.

EDIT:

Then, looping through your code it would look like this:

private void timer1_Tick(object sender, EventArgs e)
{
   var labels = this.Form1.Controls.OfType<Label>()
   foreach(var label in labels)
   {
      if(player.Bounds.IntersectsWith(label.Bounds))

//...

OTHER TIPS

As for me it is good decision to create array or even list (witch you can dynamically update) of all your 30+ labels and then to manipulate them in one short method. Of course, if the actions as much simple as you write up and nothing else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top