【发布时间】:2013-07-06 12:57:44
【问题描述】:
我正在旋转一个精灵 (rect) 以面对屏幕上的触摸位置。代码有效,但有几度的偏移量,精灵越垂直,偏移量越大。我在下面添加了一张图片,非常清楚地说明了我的问题。
红点是我在photoshop中添加的触摸位置,用来显示偏移问题。
所以唯一可见的精灵是矩形(IE rect)。
当我处于 90 度角时,偏移最为明显。然后越靠近中心,它又逐渐消失。
我怎样才能使精灵准确地面向触摸位置? 或者我该如何纠正这个偏移量?
- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
float angle = atan2f(location.y-rect.position.y, location.x-rect.position.y);
angle = CC_RADIANS_TO_DEGREES(angle);
angle *=-1;
rect.rotation = angle;
float distance = sqrtf(pow((location.x-rect.position.x), 2)+pow(location.y-rect.position.y, 2));
rect.scaleX = distance;
}
【问题讨论】:
-
什么是矩形?它与您展示的 2 个精灵有什么关系?
-
rect 是精灵。我在 Photoshop 中添加的红色圆圈以显示触摸位置与矩形角度的关系。
标签: ios objective-c cocos2d-iphone trigonometry