【问题标题】:cocos2d : rotate a sprite so it is oriented to the center of the screencocos2d : 旋转一个精灵,使其朝向屏幕的中心
【发布时间】:2012-05-02 12:43:50
【问题描述】:

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

【问题讨论】:

    标签: iphone xcode cocos2d-iphone rotation sprite


    【解决方案1】:
    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)];
            }
        }
    

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

    【讨论】:

    • 如果现在我希望它的脚朝向屏幕的中心而不是它的头部,我该怎么做?请问我需要做些什么改变?
    • 有2种方法... 1是计算角度加180.. 2nd是将原始图像旋转180度..无需更改角度.. :)
    【解决方案2】:
    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));

    【讨论】:

    • 我有错误:没有匹配'screenCenter'中的'operator' -@'property_reference_expr'不支持转储...
    • Greg,第一行CGPoint screenCenter你用的是什么?您是否将其设置为屏幕(或主窗口)的中心?
    • 我愿意:CGPoint screenCenter=ccp(240,160);
    • 您在上面报告的错误是因为无法减去一个点。所以 CGPoint - CGPoint 是不可能的。使用其他方式获取“方向”。
    • 我用float作为screenCenter但它不起作用?我该怎么办 ?能给我个代码吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2014-01-19
    • 2011-12-27
    • 1970-01-01
    相关资源
    最近更新 更多