简体   繁体   English

如何旋转精灵并将其移向光标?

[英]How to rotate and move a sprite towards the cursor?

Lets say that I have a player sprite, located at (player.x, player.y). 假设我有一个播放器精灵,位于(player.x,player.y)。 On a mousemove event, I am capturing the cursor's position, which is (e.pageX, e.pageY). 在mousemove事件中,我正在捕获光标的位置,即(e.pageX,e.pageY)。 In the game, the player will be constantly moving, always towards the direction of the cursor. 在游戏中,玩家将不断朝着光标方向移动。 I am trying to find a way to to rotate the player sprite to always face the mouse cursor, and also move 10 pixels closer to the cursor every game tick. 我正在尝试找到一种方法来旋转玩家精灵,使其始终面对鼠标光标,并且在每次游戏刻度时也将光标移动10像素。 So far, this has me stumped. 到目前为止,这让我感到困惑。 I have seen many examples of this online, and have tried all of them, but none seem to work. 我在网上看到了许多示例,并尝试了所有示例,但似乎都没有用。 Any help that can be provided would be very much appreciated. 可以提供的任何帮助将不胜感激。

First of all, you must learn the basics of trigonometry. 首先,您必须学习三角学的基础知识。 Without this knowledge, you will can't make something like you want. 没有这些知识,您将无法做出想要的东西。

Center of you image is the center of an circle. 您的图像中心是一个圆的中心。 Knowing position of center and mouse position, you can calculate the angle. 知道中心位置和鼠标位置,就可以计算角度。 Calculated angle you can use to rotate image. 可以用来旋转图像的计算角度。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM