Objective c 围绕圆移动节点
我正在围绕使用以下代码创建的圆移动我的sknode:Objective c 围绕圆移动节点,objective-c,xcode,sprite-kit,uibezierpath,sknode,Objective C,Xcode,Sprite Kit,Uibezierpath,Sknode,我正在围绕使用以下代码创建的圆移动我的sknode: circleDiameter = 300; pathCenterPoint = CGPointMake(self.position.x - circleDiameter/2, self.position.y - circleDiameter/2); UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(pathCen
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命令运行相同的操作来实现这一点
但动作总是从起点重新开始
我想知道是否有某种方法可以让用户点击屏幕时从旧动画的位置开始动画?A
UIBezierPath
由路径元素组成,其中第一个元素是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);
但是如何从圆中检索所有元素呢?我应该在bezierPoints中插入哪些点?您必须使用CGPathAddArc,详细信息请查看以下答案:@Paulo Furlan,或者您只需设置一个offset=true