【问题标题】:Move a SKNode around a circle围绕一个圆圈移动一个 SKNode
【发布时间】:2016-10-07 20:57:47
【问题描述】:

我正在围绕使用此代码创建的圆圈移动我的 sknode:

    circleDiameter = 300;
    pathCenterPoint = CGPointMake(self.position.x - circleDiameter/2, self.position.y - circleDiameter/2);

    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(pathCenterPoint.x, pathCenterPoint.y, circleDiameter, circleDiameter) cornerRadius:circleDiameter/2];
    self.actionClockwise = [SKAction followPath:circlePath.CGPath asOffset:false orientToPath:true duration:2];
    self.circleActionForever = [SKAction repeatActionForever:self.actionClockwise];
    [self runAction:self.actionCounterClockwise withKey:@"circleActionForever"];

一切正常。现在我希望当用户点击屏幕以恢复方向并逆时针移动节点时。我通过使用 .reversedAction 命令运行相同的操作来做到这一点。

但动作总是从起点重新开始。

我想知道是否有某种方法可以从用户点击屏幕时旧动画的位置开始动画?

【问题讨论】:

    标签: objective-c xcode sprite-kit uibezierpath sknode


    【解决方案1】:

    UIBezierPath 由路径Elements 组成,其中第一个是moveToPoint,如official document 中所述,在可变图形路径中的指定位置开始新的子路径

    所以,不幸的是,这样做还不够:

    UIBezierPath *circlePathReversed = [circlePath bezierPathByReversingPath];
    

    因为当你停止你的圆圈跟随路径时,圆圈的实际位置与moveToPoint点(坐标x和y)不同。

    但是,您可以重建路径以检索所有元素并从实际的圆圈位置重新开始。

    void MyCGPathApplierFunc (void *info, const CGPathElement *element) {
        NSMutableArray *bezierPoints = (__bridge NSMutableArray *)info;
    
        CGPoint *points = element->points;
        CGPathElementType type = element->type;
    
        switch(type) {
            case kCGPathElementMoveToPoint: // contains 1 point
                [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
                break;
    
            case kCGPathElementAddLineToPoint: // contains 1 point
                [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
                break;
    
            case kCGPathElementAddQuadCurveToPoint: // contains 2 points
                [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
                [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
                break;
    
            case kCGPathElementAddCurveToPoint: // contains 3 points
                [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
                [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
                [bezierPoints addObject:[NSValue valueWithCGPoint:points[2]]];
                break;
    
            case kCGPathElementCloseSubpath: // contains no point
                break;
        }
    }
    

    用法

    NSMutableArray *bezierPoints = [NSMutableArray array];
    CGPathApply(circlePath.CGPath, bezierPoints, MyCGPathApplierFunc);
    

    【讨论】:

    • 但我怎样才能从圆圈中检索所有元素?我应该在 bezierPoints 中插入哪些点?
    • 你必须使用CGPathAddArc,查看这个答案了解详情:stackoverflow.com/questions/5183785/…
    • @Paulo Furlan,或者你只是设置 asOffset = true
    猜你喜欢
    • 2021-06-24
    • 2019-09-04
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多