我不熟悉使用Control.Tag. 我只知道它可以分配给任何对象,但我不明白如何在另一个方法中调用它?我是新手,如果这个问题听起来很愚蠢,我深表歉意。
这个想法是创造一个会自行移动的敌人AI。问题是我希望通过代码而不是设计来创建敌方单位。我已经看到我可以使用for each controltype of code 和 use Control.Tag,但是如果我在其他形式中调用它,我似乎无法使其工作。
我的问题是:我是否将自定义方法controlloop()放在表单中,InitializeComponent()或者可以放在其他任何地方?如果它没有帮助,我如何让它在表单中的所有方法上公开可用?
(再次,我很抱歉这个问题听起来很愚蠢,但我想从某人的经验中获得帮助并学习它)
下面的代码是一个用于循环控制控件的自定义方法。
// checking controls
private void controlLoop()
{
// for each assigned control of all controls
foreach (Control ctrl in this.Controls)
{
// find 'picturebox' controls
if (ctrl is PictureBox)
{
// detect which one is which
switch (ctrl.Tag)
{
// if the picturebox is an enemy
case "Enemy":
EnemyAI(ctrl);
break;
default:
MessageBox.Show("Incorrect control type. Please assign it into controls first.");
break;
}
}
}
}
下面的代码是我试图解决但不能解决的问题。我尝试使用.left位置定位,但在检查所有控件后,我不明白该放什么让敌方单位移动。
// Move the enemy
private void EnemyAI(Control ctrl)
{
ctrl.Tag.left += Xvel_enemy;
}