【问题标题】:Rotating a Cocos2d sprite to match a joystick旋转 Cocos2d 精灵以匹配操纵杆
【发布时间】:2011-12-05 00:47:38
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios cocos2d-iphone rotation joystick


    【解决方案1】:

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

    - (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
                             );
    }
    

    【讨论】:

    • 说你从哪里得到这个公式?它使用 cocos 在我的代码中工作,但它是颠倒的
    【解决方案2】:

    你试过了吗:

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

    获取上次更新和当前更新之间的角度,如果你想要它让角色有设定的转身时间,你可以通过角度来缩放你的持续时间。

    【讨论】:

    • 我会试一试,但是当 CCRotate 在添加另一个 CCRotate 时尚未完成时,是什么阻止 CCRotate 相互冲突?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多