旋转雪碧与触摸 - Cocos2d [英] Rotating Sprite with Touch - Cocos2d
问题描述
有没有人有更好的方法用一个手指旋转sprite?我的问题是,我不能得到精灵停止旋转后,完全旋转两次,我翻转我的屏幕180度周期(self.rotation = 180;)然后翻转(self.rotation = 0)。
Does anyone have a better way to rotate a sprite with one finger? My problem is that I can not get the sprite to stop rotating after it has been fully rotated twice and I flip my screen 180 degree periodcally (self.rotation = 180;) then flip it back (self.rotation = 0). But, when I flip it to 180 degrees the sprite will not rotate properly.
任何人都有比这更好的想法吗?
Any one have any better ideas than this?
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
I went in to GameConfig.h and changed #define GAME_AUTOROTATION kGameAutorotationUIViewController
to #define GAME_AUTOROTATION kGameAutorotationNone
然后,进入AppDelegate.m并更改 #if GAME_AUTOROTATION == kGameAutorotationUIViewController
至 #if GAME_AUTOROTATION == kGameAutorotationNone
Then, went in to AppDelegate.m and changed #if GAME_AUTOROTATION == kGameAutorotationUIViewController
to #if GAME_AUTOROTATION == kGameAutorotationNone
当我翻转屏幕时固定精灵的旋转,但是我仍然有问题,在两个完整旋转后停止精灵的旋转。
That fixed the sprite's rotation when I flipped the screen, but I am still having problems stopping the sprite's rotation after two full rotations.
推荐答案
在结尾添加一行:
gRotation += currentTouch - previousTouch;
gRotation = fmod(gRotation,360.0); // <<< fix the angle
这可能解决了你的问题,因为角度会保持在360°范围
this maybe fix your problem, because the angle will stay in the 360 range
用一个手指旋转的其他方法是UIPanGestureRecognizer(但是你仍然需要保持角度在360范围内):
Other way to rotate with one finger is the UIPanGestureRecognizer (but you still need to keep the angle in the 360 range):
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];
...
}
查看本教程了解更多详情(它的拖动,但显示如何做平移手势):
Take a look at this tutorial for more details (its on drag, but show how to do the pan gesture):
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
这篇关于旋转雪碧与触摸 - Cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!