简体   繁体   English

C#图片框不起作用

[英]C# Picture box isnt working

I have a picture box that can move around and I added a function to add random picture boxes to my form and when the movable picture box intersects with one of the random ones its supposed to start another form but it isn't working. 我有一个可以移动的图片框,并且添加了一个向窗体添加随机图片框的功能,当活动图片框与任意一个随机相交时,它应该启动另一种窗体,但无法正常工作。 Can anyone help me understand why it is not? 谁能帮助我了解为什么不是这样?

Random rand = new Random();
int pb = rand.Next(1, 9);

List<PictureBox> _enemies = new List<PictureBox>();

PictureBox pbRandom = new PictureBox();
int x = rand.Next(1, 1250);
int y = rand.Next(1, 760);
pbRandom.Location = new Point(x, y);

_enemies.Add(pbRandom);

foreach(PictureBox pictureb in _enemies)
{
    panel1.Controls.Add(pictureb); 
    pictureb.Image = Properties.Resources._200w_d;
    if (pictureBox1.Location == pictureb.Location)
    {
        Form bs = new Battle_Screen();
        bs.ShowDialog(); 
    }
}

You're checking if two PictureBox es have exact same location, not if they intersect each other. 您正在检查两个PictureBox是否具有完全相同的位置,而不是它们是否相交。

To do that, change line 为此,请更改行

if (pictureBox1.Location == pictureb.Location)

with

if (pictureBox1.Bounds.IntersectsWith(pictureb.Bounds))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM