我正在做一个游戏,你点击射击朝光标过得弹射击弹丸。但是,我不知道如何去做使弹丸向光标方向飞行。我试图找到像这样的斜线...使用光标
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