【发布时间】:2011-09-20 05:00:44
【问题描述】:
我知道这个问题已经被问过好几次了,相信我我已经搜索过了。我找到了一个用触摸旋转精灵的答案,但必须有一种更简单的方法。
我所需要的只是让我的精灵随着我的触摸而旋转。最大旋转 0 最小旋转 0。
我知道我需要进行一些检查。
int maxRot = 0;
int minRot = 0;
if (arrowRotation > maxRot)
{
//do or don't do something
} else if (arrowRotation < minRot)
{
//do or don't do something
}
有人可以引导我以正确的方向通过触摸旋转精灵,最小和最大旋转吗?
这是我认为比较复杂或可以用更简单的方式完成的代码。
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, _arrow.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
arrowRotation += currentTouch - previousTouch;
}
提前致谢!
【问题讨论】:
-
添加一个您认为太复杂的解决方案的链接 - 这可能是唯一的方法;)
-
我用代码更新了我的问题!
-
maxRot 和 minRot 都是 = 0 是什么意思。
-
箭头可以转动的最小和最大度数
-
您的代码有效吗?看起来不错。如果它运作良好,那么不要担心试图让它“更简单”。如果有问题,那么您可以对其进行调整,直到它达到您想要的为止。
标签: iphone cocos2d-iphone