我正在尝试制作一个炮塔,转向触碰的位置然后射击子弹 到目前为止没有问题,但我无法弄清楚为什么炮塔不会面对我触及的方向 注意:我忘了告诉它转向但不是我触摸的具体位置。 我看了这些消息来源,但他们没有帮助 http://www.cocos2d-iphone.org/forum/topic/1823 http://stackoverflow.com/questions/3018688/rotating-and-moving-a-uiimageview-cocoatouch
这是我执行旋转的代码。我错过了什么?
-(void)update:(UITapGestureRecognizer *)recognizer
{
double dx,dy;
CGPoint location = [recognizer locationInView:self.view];
turret.transform=CGAffineTransformIdentity;
bullet.transform=CGAffineTransformIdentity;
bullet.center=turret.center;
if ( location.x < 160 )
{
dx=turret.center.x-location.x;
dy=turret.center.y-location.y;
//if user touched greater than 180
}
else if ( location.x > 160 )
{
dx=location.x-turret.center.x;
dy=location.y-turret.center.y;
//if user touched less than 180
}
CGAffineTransform transform = CGAffineTransformMakeRotation((atan2(dy,dx)*180/M_PI));
NSLog(@"%f",atan2(dy,dx)*180/M_PI);
bullet.transform=transform;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
turret.transform = transform;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
bullet.center=location;
[UIView commitAnimations];
答案 0 :(得分:1)
我相信CGAffineTransformMakeRotation使用的是弧度而不是度数,所以不要将atan2(dy,dx)的输出乘以180 / pi(尽管你可以在NSLog的时候将它记录下来,如果你希望以度数记录)。