iphone - 旋转 Sprite 面对一个点(cocos2d)

标签 iphone ios ipad cocos2d-iphone

我似乎在计算我的 sprite 和触摸点之间的角度时遇到了问题。每当用户触摸屏幕时,我都试图让我的 Sprite 直接面对触摸点的方向。这是我的代码:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    CGPoint tapPosition;
    for (UITouch *touch in touches){
        CGPoint location = [touch locationInView:[touch view]];
        tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]];
    }

    float angle = CC_RADIANS_TO_DEGREES(ccpAngle(fish.position, tapPosition));
    [fish runAction:[CCRotateTo actionWithDuration:0.5 angle:angle]];
}

有什么想法吗?谢谢

最佳答案

将此添加到 Nikhil 的答案末尾,以避免在触摸位置位于 Sprite 右下角时获得负角度。

if (calculatedAngle < 0) 
{
     calculatedAngle+=360;
}

关于iphone - 旋转 Sprite 面对一个点(cocos2d),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10614876/

相关文章:

objective-c - 100% 不透明度 UILabel 超过 50% 不透明度背景(UIView?)

iphone - 从 UITableViewCell 到模态视图 Controller 的动画

iphone - GPPShareBuilderImpl getURL 内容深层链接 ID 需要带有标题和描述

iphone - IOS 4.3 如何正确解析来自 Web 服务的 Json 响应并将其绑定(bind)到 UITABLEVIEW

iphone - 未使用的变量和路径

objective-c - 导航 Controller "back button"文本 : Presented Modally

objective-c - 自动创建的 NSManagedObject 子类不使用 ARC

javascript - Cordova 相机插件在 iOS 上崩溃

iphone - openAL 获取/设置偏移量(以秒为单位)

iphone - Objective-c:如何检测双击 View ?