所以我有一個精靈節點,我想通過跟蹤用戶的觸摸旋轉它... 這是我做過什麼:使用旋轉觸摸精靈數據
- (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];
}
這是旋轉的,但方式太快...任何線索爲什麼?
對於題外話題很抱歉,但是當p2.x == p1.x'時'slopeBetweenPoints(CGPoint p1,CGPoint p2)'中的0問題除外。 – RaffAl
是的,這是真的......謝謝指出。 – user1028028
你是否嘗試過增加持續時間,比如說2 *持續時間 – santhu