# 基于鼠标单击XNA的运动

[英]Movement based on mouse click XNA

``````        if (this.order == "Move")
{
if (Math.Abs(this.orderPos.X - this.warlockPos.X) < this.moveSpeed && Math.Abs(this.orderPos.Y - this.warlockPos.Y) < this.moveSpeed)
{
this.warlockPos = this.orderPos;
this.order = "None";
this.orderPos = Vector2.Zero;
}
else
{
delta = this.orderPos - this.warlockPos;
if (delta.Y == 0)
{
if (delta.X < 0)
angle = -90;
else
angle = 90;
}
else
{
if (delta.Y < 0)
angle = 180 + Math.Atan(delta.X / delta.Y);
else
angle = Math.Atan(delta.X / delta.Y); ;
}
this.warlockPos = new Vector2(this.warlockPos.X + this.moveSpeed * (float)Math.Sin(angle), this.warlockPos.Y + this.moveSpeed * (float)Math.Cos(angle));
}
}
``````

this.order == move表示客户的最后订单是移动。 orderPos是客户端最后告诉服务器角色应该移动的地方。 warlockPos是角色的当前位置（上一次更新结束时的位置）。 moveSpeed只是它移动的速度。 我使用增量和角度来确定角色在此更新中应移至的位置。

``````if (order == "Move")
{
Vector2 delta = orderPos - warlockPos;

if (delta.Length() < moveSpeed)
{
warlockPos = orderPos;
order = "None";
orderPos = Vector2.Zero;
}
else
{
delta.Normalize();
delta *= moveSpeed;
warlockPos += delta;
}
}
``````

© 2020-2024 STACKOOM.COM