使用旋转触摸精灵数据
问题描述:
所以我有一个精灵节点,我想通过跟踪用户的触摸旋转它... 这是我做过什么:使用旋转触摸精灵数据
- (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];
}
这是旋转的,但方式太快...任何线索为什么?
答
我认为问题出在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
答
基于一些答案,我发现在这里(在这个问题上的边栏) 解决办法:
- (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));
}
}
这比我想象的简单。
对于题外话题很抱歉,但是当p2.x == p1.x'时'slopeBetweenPoints(CGPoint p1,CGPoint p2)'中的0问题除外。 – RaffAl
是的,这是真的......谢谢指出。 – user1028028
你是否尝试过增加持续时间,比如说2 *持续时间 – santhu