Cocos2D :旋转角色,使其朝向屏幕中心



所以我每秒在随机位置创建一个精灵。我想要的是精灵朝向屏幕中心。例如,如果我每秒在随机位置创建一个人体,我希望他的头部朝向屏幕中心。谢谢:)对不起我的英语我是法国人:/

CCPoint pos1 = [yourHuman position];
CCPoint pos2 = screenCenter;
    float theta = atan((pos1.y-pos2.y)/(pos1.x-pos2.x)) * 180 * 7 /22;
    if(pos1.y - pos2.y > 0)
    {
        if(pos1.x - pos2.x < 0)
        {
            [yourHuman setRotation:(-90-theta)];
        }
        else if(pos1.x - pos2.x > 0)
        {
            [yourHuman setRotation:(90-theta)];
        }       
    }
    else if(pos1.y - pos2.y < 0)
    {
        if(pos1.x - pos2.x < 0)
        {
            [yourHuman setRotation:(270-theta)];
        }
        else if(pos1.x - pos2.x > 0)
        {
            [yourHuman setRotation:(90-theta)];
        }
    }

这已经完成了。将此代码保存在方法中。并使用它..:)希望这有帮助..

CGPoint screenCenter = ...; // set it manually or from device screen size
CGPoint direction = ccpSub (screenCenter, yourHuman.position);
yourHuman.rotation = CC_RADIANS_TO_DEGREE (ccpToAngle (direction));

我不确定旋转角度,所以也许你需要

yourHuman.rotation = -CC_RADIANS_TO_DEGREE (ccpToAngle (direction));

最新更新