【发布时间】:2018-07-31 21:21:04
【问题描述】:
我正在制作一个服务器/客户端游戏,其动作类似于魔兽争霸 1/2/3 等战略游戏。在该游戏中,玩家通过右键单击该区域的某个位置来移动,而角色会尝试移动到那里。我试图让角色始终朝着该点直线前进,直到它到达那里(或足够接近)。我现在使用的代码以一种奇怪的方式在那里获取了字符。
当我单击角色下方的某个位置(较高的 Y 坐标)时,它会沿直线移动,但是当我在其上方(较低的 Y 坐标)单击时,角色会移动,就像它在移动时做一个圆圈一样,但它仍然会到达那里,只是不是我想要的方式。
这是运行每次更新以移动它的代码。
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 只是它移动的速度。 我使用 delta 和 angle 来确定角色在这次更新中应该移动的位置。
那么,当角色移动的点在其上方时,是什么让角色以圆周方式移动?
【问题讨论】:
标签: c# xna mouseevent