我正在制造太空侵略者,我希望我的子弹从我的大炮所在的位置出来。当我按空格键时,子弹会发射,但我每次按空格键时都需要它能够访问我的 cannonX 的位置,它不允许我访问它的信息。
public void tsbtnStart_Click(object sender, EventArgs e)
{
// Make invader
Invader invaderX = new Invader();
pnlBattleField.Controls.Add(invaderX);
// Mke UFO
Ufo ufoX = new Ufo();
pnlBattleField.Controls.Add(ufoX);
// Make cannon
Cannon cannonX = new Cannon(this.pnlBattleField.Height - 80);
if (made == false)
{
pnlBattleField.Controls.Add(cannonX);
made = true;
}
Point location = cannonX.PointToScreen(Point.Empty);
tmrClock.Interval = 200;
tmrClock.Start();
tmrClock2.Interval = 100;
tmrClock2.Start();
}
public void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Space)
{
Bullet bulletX = new Bullet(this.pnlBattleField.Height - 80, location.x );
// "location does not exist in current context
pnlBattleField.Controls.Add(bulletX);
}
}