簡體   English   中英

圍繞一個圓圈移動一個 SKNode

[英]Move a SKNode around a circle

我正在圍繞使用此代碼創建的圓圈移動我的 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 命令運行相同的操作來做到這一點。

但動作總是從起點重新開始。

我想知道是否有某種方法可以從用戶點擊屏幕時舊動畫所在的點開始動畫?

UIBezierPath由路徑Elements組成,其中第一個是moveToPoint ,如官方文檔中所述,它在可變圖形路徑中的指定位置開始一個新的子路徑

因此,不幸的是,這樣做還不夠:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM