0

我看到了这个关于如何使用 SKAction 顺时针方向跟随 Circle CGPathRef 的 答案。

矩形路径呢?默认为逆时针方式:

CGPathRef square =     CGPathCreateWithRect(CGRectMake(pathOriginX,pathOriginY,pathWidthX,pathHeightY), NULL);
SKAction *followTrack = [SKAction followPath:square asOffset:NO orientToPath:YES duration:completeOnePathDuration];

尝试更改端点,但结果是相同的。

4

1 回答 1

1

简短答案:反向运行动作以沿着相反方向的路径前进

    [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;
}
于 2014-08-24T17:34:38.090 回答