如何检查从不同类创建的两个控件之间的冲突(在多个类实例中)

时间:2018-11-10 12:15:16

标签: c# winforms class instance

我正在ODataPath中制作一个简单的游戏,您必须射击以杀死敌人(都是C#(射击和敌人))。 我有一个单独的课程,仅用于镜头的创建和处理,另一个则是针对敌人的。 在我的pictureboxs中,我有enemy class(一个专门用于创建控件本身),另一个仅用于调用它(生成敌人) 我有此代码的问题,它将仅与创建的第一个类(怪物)冲突,而不与我以2 methods

形式生成的3类冲突
for loop.

在我的铅球课中(这将检查两者是否发生碰撞)

 public static PictureBox Monstro_P; //Static picturebox variable that is the enemy control
  public void spawn_Monstro(Form form, int _X, int _Y) //Method that spawns the monster
    {
        Monstro_S(form, _X, _Y);
    }
    public void Monstro_S(Form form, int _X, int _Y)//Method that creates the control
    {
        Monstro_P = new PictureBox()
        {
            BackColor = Color.Chocolate,
            Size = new Size(20, 35),
            Left = _X,
            Top = _Y,
            Tag = "Monstro"
        };

        aa.Interval = 10;
        aa.Tick += new EventHandler(aa_Timer);
        aa.Start();
        form.Controls.Add(Monstro_P);
        Monstro_PB = new ProgressBar() //Represents the health bar of the enemy
        {
           Width = Monstro_P.Width,
           Height = 10,
           Location = Monstro_P.Location,
            Maximum = vida,
           Value = vida,
        };
        Monstro_PB.Top -= 15;
        form.Controls.Add(Monstro_PB);
    }

//如果静态变量(怪物图片框)与镜头控件冲突             } 我在其中生成Monster类的Form1加载代码:

public PictureBox Tiro_P; //Public variable that is the shot control
    public void Spawn_Tiro(Form form, int _X, int _Y)//Method that spawns te shot 
        {
            Tiro_C(form, _X, _Y);
        }
        public void Tiro_C(Form form, int _X, int _Y)//Method that creates the control
        {
            Tiro_P = new PictureBox()
            {
                Left = _X,
                Top = _Y,
                Size = new Size(10, 8),
                BackColor = Color.Crimson,
                Tag = "Tiro"
            };
            Tiro_P.LocationChanged += new EventHandler(Tiro_Localizao); //PictureBox LocationChanged event to check when it hits
            Tiro_T.Interval = 10;
            Tiro_T.Start();
            Tiro_T.Tick += new EventHandler(Tiro_Timer);
            form.Controls.Add(Tiro_P);
        }
        void Tiro_Timer(object sender, EventArgs e)//Timer for the class
        {
            Tiro_P.Left += 5; //Moves the picturebox(shot in this case)

        }
        void Tiro_Localizao(object sender, EventArgs e)//LocationChanged event
        {

            if (Monstro.Monstro_P.Bounds.IntersectsWith(Tiro_P.Bounds))
                MessageBox.Show("you got hit!");

我每次单击表单时都会生成镜头的代码:

   private void Form1_Load(object sender, EventArgs e)
        {
            for (int a = 0; a < 3; a++) //Spawns a monster 3 times in random locations
            {
                var x = rnd.Next(50, 550);
                var y = rnd.Next(50, 550);
                m = new Monstro();
                m.spawn_Monstro(this, x, y);
            }
        }

编辑: 我在计时器上放置了完全相同的代码,因为我认为PictureBox LocationChanged无法正常工作,但也无法正常工作。

0 个答案:

没有答案