我的第一篇文章,希望得到一些帮助,我似乎无法弄清楚问题。对于我应该做的任务,我需要创建一个有2个玩家互相攻击的游戏。我希望用户能够在每轮之后升级他们的统计数据,但是以与游戏形式分开的形式升级它们(分别称它们为UpgradeForm和GameForm)。
我保留了大多数玩家'列表中Helicopters类中的数据(List Heli = new List();)我最大的问题是尝试将该列表从GameForm调用到我的UpgradeForm中。以下是我到目前为止所获得的代码:
public partial class GameForm : Form
{
public List<Helicopter> Heli = new List<Helicopter>();
private void GameForm_Load(object sender, EventArgs e)
{
Heli.Add(new Helicopter(1, this));//give helicopter player # and set a form variable to the current GameForm
Heli.Add(new Helicopter(2, this));
}
private void GameForm_to_UpgradeForm()
{
newForm = true;
UpgradeForm UF = new UpgradeForm(this);
UF.Show();
FormProvider.GameForm.Hide();
//FormProvider is how i'm calling my GameForm.
//Shouldn't have an effect on what I'm trying to do.
}
}
在我的UpgradeForm中:
public partial class UpgradeForm : Form
{
private GameForm gF;
List<Helicopter> HeliList = new List<Helicopter>();
public UpgradeForm(GameForm gForm)
{
InitializeComponent();
this.gF = gForm;
HeliList = this.gF.Heli;
}
}
然而,我收到一个错误: &#34;错误1可访问性不一致:字段类型&#39; System.Collections.Generic.List&#39;比字段&Project; GameForm.Heli&#39;
更容易访问我做错了什么,我需要修理什么?如果需要,会将代码发布到FormProvider类。提前谢谢。
答案 0 :(得分:1)
修改你的UpgradeForm:
public partial class UpgradeForm : Form
{
private GameForm gF;
List<Helicopter> HeliList = new List<Helicopter>();
public UpgradeForm(List<Helicopter> list)
{
InitializeComponent();
HeliList.AddRange(list);
}
}
来自GameFprm课程:
private void GameForm_to_UpgradeForm()
{
newForm = true;
UpgradeForm UF = new UpgradeForm(Heli);
UF.Show();
FormProvider.GameForm.Hide();
}
“我做错了什么,我需要修理什么?”