我正在製造太空入侵者,我希望我的子彈能夠從我的大炮的位置出來。當我按下空間時,子彈將會射擊,但我需要它能夠在每次按下空間時訪問我的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);
}
}
你需要得到一個參考'Cannon'對象是這樣的形式,這是目前在某處'pnlBattleField.Controls'上。 – David 2013-04-23 16:55:07
您需要引用該對象;所以你可以訪問。您還必須傳遞X軸和Y軸上的座標值。 – Greg 2013-04-23 16:55:59
如何引用對象? – 2013-04-23 18:16:56