所以,我发现一些代码基本上有一个矩形跟随鼠标在我的播放器对象周围设定距离。在2D平面上,我有角色,当玩家点击鼠标按钮时,鼠标和角色之间会出现一个盾牌。这部分有效,这里是代码:
var angle;
var track_radius = 10;
// find the angle to the mouse
angle = point_direction(self.x, self.y, mouse_x, mouse_y);
// turn the shield to look at the mouse
image_angle = angle;
// convert to radians for the math functions
angle = degtorad(angle);
// place the shield on a circular track about the player
// . track_radius is set in the create event
x = global.playerX + 50 * cos(angle);
y = global.playerY - 50 * sin(angle); // minus
因此,当鼠标接近屏蔽时会出现问题。屏蔽分成两部分,镜像在鼠标指针上,来回闪烁。当鼠标靠近或存在护盾的半径范围内时,问题基本上继续存在。同样值得注意的是,当单击鼠标时,屏蔽是在播放器的位置创建的,然后使用上面的代码移动。思考?
答案 0 :(得分:0)
angle = point_direction(self.x, self.y, mouse_x, mouse_y);
此代码计算从 shield 到鼠标指针的角度,因为self
当前指的是盾牌。
x = global.playerX + 50 * cos(angle);
y = global.playerY - 50 * sin(angle); // minus
在此设置盾牌相对于玩家的位置,以便从玩家到盾牌的角度等于您在上面计算的角度。
现在考虑鼠标靠近护罩的情况,从护罩到鼠标的方向与从播放器到鼠标的方向不同。在下面的坏ASCII图形中,假设O是播放器,()是屏蔽,而^是鼠标光标。
O ------ )
^
屏蔽直接位于播放器右侧(0°),但鼠标直接位于屏蔽下方(270°)。因此,您的代码会将屏蔽直接放在播放器下方(请记住,代码中的angle
是从屏蔽到鼠标的方向)
O
| ^
|
|
)
在下一步中,代码再次采用从屏蔽到鼠标的方向。这次它更像是盾牌上方45°,因此盾牌将被放置在玩家的上方和右侧45°处。
这种情况会随着位置的变化而来回跳动几次,直到盾牌“固定”在鼠标光标周围的两个交替位置。
要找到解决方案,您需要使用播放器到鼠标光标的方向来确定屏蔽位置,而不是从屏蔽到鼠标光标的方向。但我不得不说,你意外创建的系统的动态有点有趣:)