简短答案:反向运行动作以沿着相反方向的路径前进
[sprite runAction:[followTrack reversedAction]];
长答案: followPath SKAction 遵循构建路径的方向。如果您希望您的精灵沿顺时针或逆时针方向沿着矩形路径移动,则相应地构建路径。或者,您可以使用 reverseAction 方法沿着与构建方向相反的路径前进。以下是这两种方法的示例。
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)
- (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;
}