【发布时间】:2018-06-18 18:58:51
【问题描述】:
我正在尝试用 c# 编写一个小游戏,该游戏涉及移动敌人。 这些敌人是使用以下代码生成的,此代码多次使用以生成多个敌人。
private void EventHandler(Action<object, EventArgs> spawnBox)
{
Random randomPlek = new Random();
int xPlek;
xPlek = randomPlek.Next(1000, 1100);
int yPlek;
yPlek = (randomPlek.Next(0, 8)) * 100;
var picture = new PictureBox
{
Name = "pictureBoxLM",
Size = new Size(150, 100),
SizeMode = PictureBoxSizeMode.StretchImage,
BackColor = Color.Transparent,
Location = new Point(xPlek, yPlek),
Image = Leeuwenmier,
};
this.Controls.Add(picture);
}
问题是当试图让它们移动或碰撞时,Visual Studio 找不到名称并给出错误。这是我用于碰撞的代码:
if(PbMier.Bounds.IntersectsWith(pictureBoxLM.Bounds))
{
// some actions
}
如何在代码中调用生成的图片框而不报错?
【问题讨论】: