2011-05-31 97 views
0

有沒有人有更好的方法來用一個手指旋轉雪碧?我的問題是我不能讓精靈在完全旋轉兩次後停止旋轉,並且我週期性地將我的屏幕翻轉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

當我翻轉屏幕時,修正了精靈的旋轉,但是在兩次完整旋轉後我仍然無法停止精靈的旋轉。

回答

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

+0

是否有不同的方法來旋轉精靈?因爲grinderRotation等級取決於currentTouch和previousTouch – Jonathan 2011-06-03 18:30:50

+0

您可以使用UIPanGestureRecognizer類來檢測只有一個手指(iOS 4或更高版本)的拖動。 – 2011-06-03 19:22:58

+0

我在答案中加入了一個例子... – 2011-06-03 19:29:06