有没有人有更好的方法用一根手指旋转精灵?我的问题是,我无法让精灵在完全旋转两次后停止旋转,我将屏幕周期旋转180度(self.rotation = 180;)然后将其翻转(self.rotation = 0)。但是,当我将它翻转到180度时,精灵将无法正常旋转。
任何人都有比这更好的想法吗?
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
当我翻转屏幕时修正了精灵的旋转,但是在两次完整旋转后我仍然无法停止精灵的旋转。
答案 0 :(得分:0)
在最后添加一个新行:
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