我正在做一個遊戲,你點擊射擊朝光標過得彈射擊彈丸。但是,我不知道如何去做使彈丸向光標方向飛行。我試圖找到像這樣的斜線...使用光標
public bool PreFilterMessage(ref Message m)
{
if(m.Msg == 0x0201)
{
Point MousePos = PointToClient(Form.MousePosition);
slope = (MousePos.Y - aCharacter.Location.Y)/(MousePos.X - aCharacter.Location.X);
aLaser.Location = aCharacter.Location;
if (MousePos.X < aCharacter.Location.X)
lasDir = -1;
else
lasDir = 1;
laserLaunched = true;
return true;
}
private void aChMvmTimer_Tick(object sender, EventArgs e)
{
if(laserLaunched)
{
aLaser.Location = new Point(aLaser.Location.X + lasDir, aLaser.Location.Y + (slope * lasDir));
aLaser.Visible = true;
}
else
{
aLaser.Visible = false;
}
}
但是,它沒有在光標處拍攝,並且在拍攝的地方几乎是隨機的。我會如何去做這件事?
我嘗試另一種解決辦法,但我懷疑,我做的對。
int currentTime = Environment.TickCount;
double deltaTime = (currentTime - lastTime)/1000.0;
lastTime = currentTime;
if (laserLaunched)
{
int movementSpeed = 20;
//aLaser.Location = new Point(aLaser.Location.X + lasDir, aLaser.Location.Y + (slope * lasDir));
aLaser.Visible = true;
double x = aLaser.Location.X - curMousePos.X;
double y = aLaser.Location.Y - curMousePos.Y;
double loc = ((int)x^2) + ((int)y^2);
loc = Math.Sqrt(((int)x^2) + ((int)y^2));
x = (x/(int)loc);
y = (y/(int)loc);
x *= (int)deltaTime * movementSpeed;
y *= (int)deltaTime * movementSpeed;
aLaser.Location = new Point(aLaser.Location.X + (int)x, aLaser.Location.Y + (int)y);
}
我做對了嗎?
「它不工作」 是沒有問題的描述。而「最細微」的限定詞根本沒有改善。 –
感謝您指出了這一點!當我發佈時我沒有意識到。 – Beldar4000