2014-02-22 76 views
0

所以我有一個精靈節點,我想通過跟蹤用戶的觸摸旋轉它... 這是我做過什麼:使用旋轉觸摸精靈數據

- (void)calculateAngleAndRotateSelectedCellForThouchPoint:(CGPoint)tPoint withDuration:(NSTimeInterval) duration 
{ 
    CGPoint cellPoint = self.currentlySelectedCell.position; 
    // math here 
    if (!CGPointEqualToPoint(tPoint, previousTouchPoint)) { 
     float rotationAngle = radiansFromSlope(slopeDifference(cellPoint, tPoint, cellPoint, previousTouchPoint)); 
     SKAction *rotateForAimingAction = [SKAction rotateByAngle:rotationAngle duration:duration]; 
     [self.currentlySelectedPlayer runAction:rotateForAimingAction]; 
    } 
} 

    #pragma mark - Trigonometric calculations (C functions) 

    static inline float slopeBetweenPoints(CGPoint p1, CGPoint p2) 
    { 
     return ((p2.y-p1.y)/(p2.x-p1.x)); 
    } 

    static inline float slopeDifference(CGPoint pl1_1, CGPoint pl1_2, CGPoint pl2_1, CGPoint pl2_2) 
    { 
     return (slopeBetweenPoints(pl1_1, pl1_2) - slopeBetweenPoints(pl2_1, pl2_2)); 
    } 

    static inline float radiansFromSlope(float slope) 
    { 
     return atanf(slope); 
    } 

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     touchStartTime = event.timestamp; 
     crtIntervalPrev = event.timestamp; 
    } 

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     NSTimeInterval crtThouchDuration = event.timestamp - touchStartTime; 

     if (crtThouchDuration > TAP_TIMEOUT) { 
      /// rotate the selected player sprite 
      currentTouchPoint = [[touches anyObject] locationInView:self.view]; 
      NSTimeInterval time_delta = event.timestamp - crtIntervalPrev; 
      [self calculateAngleAndRotateSelectedCellForThouchPoint:currentTouchPoint withDuration:time_delta]; 
      crtIntervalPrev = event.timestamp; 
     } 
     } 

     - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
     { 
      [self resetTouchTimes]; 
      [self resetTouchPoints]; 
      [self deselectAllCells]; 
     } 

     - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
     { 
      touchEndTime = event.timestamp; 
      NSTimeInterval touchDuration = touchEndTime - touchStartTime; 

      if (touchDuration < TAP_TIMEOUT) { 
       // it's a tap ... so deselect the selected cell 
       [self deselectAllCells]; 
      } 
      [self resetTouchPoints]; 
      [self resetTouchTimes]; 
     } 

這是旋轉的,但方式太快...任何線索爲什麼?

+0

對於題外話題很抱歉,但是當p2.x == p1.x'時'slopeBetweenPoints(CGPoint p1,CGPoint p2)'中的0問題除外。 – RaffAl

+0

是的,這是真的......謝謝指出。 – user1028028

+0

你是否嘗試過增加持續時間,比如說2 *持續時間 – santhu

回答

0

我認爲問題出在calculateAngleAndRotateSelectedCellForThouchPoint方法中。 而不是運行動作,只分配旋轉角度。

- (void)calculateAngleAndRotateSelectedCellForThouchPoint:(CGPoint)tPoint withDuration:(NSTimeInterval) duration 
{ 
    CGPoint cellPoint = self.currentlySelectedCell.position; 
    // math here 
    if (!CGPointEqualToPoint(tPoint, previousTouchPoint)) { 
     float rotationAngle = radiansFromSlope(slopeDifference(cellPoint, tPoint, cellPoint, previousTouchPoint)); 

     self.currentlySelectedPlayer.zRotation = rotationAngle; 
    } 
} 
+0

仍然無法正常工作,但效果會好很多。我在某個時間點輪換跳躍......可能是有人注意到的零點劃分 – user1028028

0

基於一些答案,我發現在這裏(在這個問題上的邊欄) 解決辦法:

- (void)calculateAngleAndRotateSelectedCellForThouchPoint:(CGPoint)tPoint 
{ 
    CGPoint cellPoint = self.currentlySelectedCell.position; 
    // math here 
    if (!CGPointEqualToPoint(tPoint, previousTouchPoint)) { 

     float dy = cellPoint.y - tPoint.y; 
     float dx = cellPoint.x - tPoint.x; 

      self.currentlySelectedCell.zRotation = - (atan2(dy,dx) - DEGREES_TO_RADIANS(90)); 
    } 
} 

這比我想象的簡單。