旋转 Cocos2d 精灵以匹配操纵杆

发布于 2024-12-19 09:56:12 字数 187 浏览 3 评论 0原文

我在 Cocos2d 中使用 SneakyJoystick,并且尝试让精灵旋转以面向与操纵杆指向相同的方向(这是自上而下)。

我可以让它旋转以面对它,但随着旋转每帧左右更新,它会卡入到位。

如何让精灵平滑地朝目标角度旋转,而不跳到目标角度?我无法弄清楚如何使用 CCRotateTo 执行此操作,因为旋转的角度可能随时发生变化。

I'm using a SneakyJoystick in Cocos2d, and I'm trying to get a sprite to rotate to face the same direction as the joystick is pointed (this is top down).

I can get it to rotate to face it, but it snaps into position as the rotation is updated every frame or so.

How can I make the sprite rotate smoothly towards the target angle, without jumping to it? I wasn't able to figure out how to do this with a CCRotateTo because the angle to rotate towards could change at any time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-12-26 09:56:12

我最终通过使用我制作的旋转方法简单地解决了这个问题,该方法在每次更新时以正确的量沿正确的方向旋转节点/精灵。

- (void)rotateNode:(CCNode*)aNode degrees:(float)targetRotation withSpeed:(float)rotationSpeed withTimeDelta:(ccTime)dt
{
    rotationSpeed = rotationSpeed * dt;

    // Convert the difference between the two angles to radians
    float diff = (targetRotation - aNode.rotation) * (M_PI / 180);
    // Find the rotation of the vector created by the sin and cos of the difference
    float rotationDifference = atan2f(sinf(diff),cosf(diff));
    // Rotate the clip accordingly
    aNode.rotation += MAX(
                         MIN(
                             (180/M_PI) * rotationDifference,rotationSpeed), -rotationSpeed
                         );
}

I ended up fixing this simply by using a rotation method that I made, which rotates the node/sprite in the correct direction at the correct amount each update.

- (void)rotateNode:(CCNode*)aNode degrees:(float)targetRotation withSpeed:(float)rotationSpeed withTimeDelta:(ccTime)dt
{
    rotationSpeed = rotationSpeed * dt;

    // Convert the difference between the two angles to radians
    float diff = (targetRotation - aNode.rotation) * (M_PI / 180);
    // Find the rotation of the vector created by the sin and cos of the difference
    float rotationDifference = atan2f(sinf(diff),cosf(diff));
    // Rotate the clip accordingly
    aNode.rotation += MAX(
                         MIN(
                             (180/M_PI) * rotationDifference,rotationSpeed), -rotationSpeed
                         );
}
我们只是彼此的过ke 2024-12-26 09:56:12

您是否尝试过:

[CCRotateBy actionWithDuration:0.5f angle:CC_DEGREES_TO_RADIANS(90.0f)];

获取上次更新与当前更新之间的角度,如果您想要角色有设定的转身时间,您可以根据角度大小来缩放持续时间。

Have you tried:

[CCRotateBy actionWithDuration:0.5f angle:CC_DEGREES_TO_RADIANS(90.0f)];

Obtain the Angle between the last Update the current Update, also if you wanted it so the character had a set time to turn around, you can scale your duration by the amount of the angle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文