旋转精灵面对一个点(cocos2d) [英] Rotate sprite to face a point (cocos2d)

查看:128
本文介绍了旋转精灵面对一个点(cocos2d)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有一个问题,计算我的sprite和触摸点之间的角度。我试图让我的精灵直接面对的触摸点的方向,每当用户触摸屏幕。这是我的代码:

I seem to be having an issue with calculating the angle between my sprite and a touch point. I'm trying to get my sprite to directly face the direction of the touch point whenever the user touches the screen. Here's my code:

-(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的回答结尾,以避免在触摸位置到达右下角时出现负角度

Add this to the end of Nikhil's answer to avoid getting a negative angle when the touch location is to the bottom right of the sprite.

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

这篇关于旋转精灵面对一个点(cocos2d)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆