用触摸旋转雪碧 - Cocos2d
问题描述:
有没有人有更好的方法来用一个手指旋转雪碧?我的问题是我不能让精灵在完全旋转两次后停止旋转,并且我周期性地将我的屏幕翻转180度(self.rotation = 180;)然后将其翻转(self.rotation = 0)。但是,当我将其翻转到180度时,精灵将无法正确旋转。用触摸旋转雪碧 - Cocos2d
任何人有比这更好的想法?
CGFloat gRotation;
- (void)update:(ccTime)delta
{
g.rotation = gRotation;
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint(g.boundingBox, location))
{
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, g.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, g.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
gRotation += currentTouch - previousTouch;
}
}
感谢
编辑:
我进去GameConfig.h,改变#define GAME_AUTOROTATION kGameAutorotationUIViewController
到#define GAME_AUTOROTATION kGameAutorotationNone
然后进去AppDelegate.m和改变#if GAME_AUTOROTATION == kGameAutorotationUIViewController
到#if GAME_AUTOROTATION == kGameAutorotationNone
当我翻转屏幕时,修正了精灵的旋转,但是在两次完整旋转后我仍然无法停止精灵的旋转。
答
末添加一个新行:
gRotation += currentTouch - previousTouch;
gRotation = fmod(gRotation,360.0); // <<< fix the angle
这也许解决您的问题,因为角将保持在360范围内
用一个手指旋转的其他方式是UIPanGestureRecognizer(但你仍然需要保持角度在360范围内):
UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePanFrom:)] autorelease];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gestureRecognizer];
...
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
...
}
看看本教程的详细信息(其上拖拉,而是如何做锅手势):
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
是否有不同的方法来旋转精灵?因为grinderRotation等级取决于currentTouch和previousTouch – Jonathan 2011-06-03 18:30:50
您可以使用UIPanGestureRecognizer类来检测只有一个手指(iOS 4或更高版本)的拖动。 – 2011-06-03 19:22:58
我在答案中加入了一个例子... – 2011-06-03 19:29:06