cocos2d:旋转精灵,使其朝向屏幕中心

时间:2012-05-02 12:43:50

标签: iphone xcode cocos2d-iphone rotation sprite

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

这样做了。将此代码保存在方法中。并使用它.. :)希望这有助于..