问题描述
|
有没有人有更好的方法来用一根手指旋转精灵?我的问题是,在将精灵完全旋转两次之后,我无法使它停止旋转,我会周期性地将屏幕翻转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
当我翻转屏幕时,可以固定精灵的旋转,但是在旋转两整圈后仍然无法阻止精灵的旋转。
解决方法
在末尾添加新行:
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