Sprite kit 如何跟随路径:对于矩形路径,顺时针方向?
我在如何使用SKAction沿顺时针方向跟随圆圈CGPathRef时看到了这一点 矩形路径呢?默认设置为逆时针方向:Sprite kit 如何跟随路径:对于矩形路径,顺时针方向?,sprite-kit,skaction,cgpathref,Sprite Kit,Skaction,Cgpathref,我在如何使用SKAction沿顺时针方向跟随圆圈CGPathRef时看到了这一点 矩形路径呢?默认设置为逆时针方向: CGPathRef square = CGPathCreateWithRect(CGRectMake(pathOriginX,pathOriginY,pathWidthX,pathHeightY), NULL); SKAction *followTrack = [SKAction followPath:square asOffset:NO orientToPath:YES
CGPathRef square = CGPathCreateWithRect(CGRectMake(pathOriginX,pathOriginY,pathWidthX,pathHeightY), NULL);
SKAction *followTrack = [SKAction followPath:square asOffset:NO orientToPath:YES duration:completeOnePathDuration];
尝试更改结束点,但结果相同。简短回答:反向运行操作,沿着相反方向的路径运行
[sprite runAction:[followTrack reversedAction]];
详细回答:followPath操作遵循路径构建的方向。如果希望精灵沿矩形路径顺时针或逆时针方向移动,请相应地构建路径。或者,可以使用反向反应方法沿与生成路径相反的方向跟随路径。下面是两种方法的示例
BOOL clockwise = YES;
BOOL reversedActionMethod = NO;
CGRect rect = CGRectMake(CGRectGetWidth(self.frame)/2-50,CGRectGetHeight(self.frame)/2-50, 100, 100);
SKAction *followTrackCW = [SKAction followPath:[self clockwiseRectanglePathWithRect:rect] asOffset:NO orientToPath:YES duration:5];
SKAction *followTrackCCW = [SKAction followPath:[self counterClockwiseRectanglePathWithRect:rect] asOffset:NO orientToPath:YES duration:5];
if (!reversedActionMethod) {
if (clockwise) {
[sprite runAction:followTrackCW];
}
else {
[sprite runAction:followTrackCCW];
}
}
else {
if (clockwise) {
[sprite runAction:[followTrackCCW reversedAction]];
}
else {
[sprite runAction: followTrackCCW];
}
}
在场景坐标中按顺时针顺序构建矩形路径(视图坐标中为CCW)
使用内置方法在场景坐标(以及视图坐标中的CW)中按逆时针顺序构造矩形路径
答案是“使用CGPathCreateWithEllipseInRect()和CGPathAddArc()代替CGPathCreateWithEllipseInRect()”,你是否尝试过使用CGPathCreateWithRect以外的东西,比如cgpathLineTopoint?明白了!感谢您努力提供简短而冗长的答案:D
- (CGPathRef) clockwiseRectanglePathWithRect:(CGRect)rect
{
CGFloat x = rect.origin.x;
CGFloat y = rect.origin.y;
CGFloat width = rect.size.width;
CGFloat height = rect.size.width;
UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(x, y)];
[bezierPath addLineToPoint: CGPointMake(x, y+height)];
[bezierPath addLineToPoint: CGPointMake(x+width, y+height)];
[bezierPath addLineToPoint: CGPointMake(x+width, y)];
[bezierPath closePath];
return bezierPath.CGPath;
}
- (CGPathRef) counterClockwiseRectanglePathWithRect:(CGRect)rect
{
UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRect:rect];
return bezierPath.CGPath;
}