如何让精灵跟随贝塞尔曲线
how to make Sprite follow bezier curve
我对 objective-c 和 sprite kit 还很陌生,但我从事游戏开发已有一段时间了。我目前正在开发一款 2d 游戏,我的敌舰在屏幕上从右向左移动。我一直在关注游戏不同部分的教程,然后在必要时添加。我找到了一个教程,其中游戏中的敌人遵循贝塞尔曲线,我已经设法在我的游戏中实现了这一点,但是由于我是贝塞尔曲线的新手,我并不完全理解它们,并且该算法使我的精灵从上到下移动但我需要他们从左到右。
我已经包含了我用来将贝塞尔曲线添加到我的精灵的代码片段关于如何让它们从右到左而不是从上到下移动的任何建议。
CGMutablePathRef cgpath = CGPathCreateMutable();
float xStart = [self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.width];
float xEnd = [self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.width];
float cp1X =[self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.width];
float cp1y = [self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.height];
float cp2x = [self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.width];
float cp2Y = [self getRandomNumberBetween:0 to:cp1y];
CGPoint s = CGPointMake(xStart, 1024.0);
CGPoint e = CGPointMake(xEnd, -100);
CGPoint cp1 = CGPointMake(cp1X, cp1y);
CGPoint cp2 = CGPointMake(cp2x, cp2Y);
CGPathMoveToPoint(cgpath, NULL, s.x, s.y);
CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
SKAction *enemyCurve = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];
CGPoint location = CGPointMake(-self.frame.size.width-asteroid.size.width, randY);
SKAction *moveAction = [SKAction moveTo:location duration:randDuration];
SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^(){
asteroid.hidden = YES;
}];
SKAction *moveAsteroidActionWithDone = [SKAction sequence:@[enemyCurve,moveAction, doneAction]];
感谢您的帮助和建议。
贝塞尔曲线用于生成两点之间的平滑曲线。
要从左向右移动路径,请在左侧选择起点并在右侧选择终点。这两个控制点决定了从左到右的路径形状。改变以下代码中的 startpoint
和 endpoint
将控制贝塞尔曲线的起点和终点。改变 control points
会改变曲线的形状。您可以通过附加的 GIF 看到这一点。
CGMutablePathRef cgpath = CGPathCreateMutable();
CGPoint startingPoint = CGPointMake(50, 100);
CGPoint controlPoint1 = CGPointMake(160, 250);
CGPoint controlPoint2 = CGPointMake(200, 140);
CGPoint endingPoint = CGPointMake(303, 100);
CGPathMoveToPoint(cgpath, NULL, startingPoint.x, startingPoint.y);
CGPathAddCurveToPoint(cgpath, NULL, controlPoint1.x, controlPoint1.y,
controlPoint2.x, controlPoint2.y,
endingPoint.x, endingPoint.y);
SKAction *enemyCurve = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];
[enemy runAction:[SKAction sequence:@[[SKAction waitForDuration:1],enemyCurve]]];
P0
和P3
是起点和终点,P1
和P2
是控制点。
查看此页面以更多地使用贝塞尔曲线。
http://www.jasondavies.com/animated-bezier/
我对 objective-c 和 sprite kit 还很陌生,但我从事游戏开发已有一段时间了。我目前正在开发一款 2d 游戏,我的敌舰在屏幕上从右向左移动。我一直在关注游戏不同部分的教程,然后在必要时添加。我找到了一个教程,其中游戏中的敌人遵循贝塞尔曲线,我已经设法在我的游戏中实现了这一点,但是由于我是贝塞尔曲线的新手,我并不完全理解它们,并且该算法使我的精灵从上到下移动但我需要他们从左到右。
我已经包含了我用来将贝塞尔曲线添加到我的精灵的代码片段关于如何让它们从右到左而不是从上到下移动的任何建议。
CGMutablePathRef cgpath = CGPathCreateMutable();
float xStart = [self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.width];
float xEnd = [self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.width];
float cp1X =[self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.width];
float cp1y = [self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.height];
float cp2x = [self getRandomNumberBetween:0+asteroid.size.width to:self.frame.size.width-asteroid.size.width];
float cp2Y = [self getRandomNumberBetween:0 to:cp1y];
CGPoint s = CGPointMake(xStart, 1024.0);
CGPoint e = CGPointMake(xEnd, -100);
CGPoint cp1 = CGPointMake(cp1X, cp1y);
CGPoint cp2 = CGPointMake(cp2x, cp2Y);
CGPathMoveToPoint(cgpath, NULL, s.x, s.y);
CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
SKAction *enemyCurve = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];
CGPoint location = CGPointMake(-self.frame.size.width-asteroid.size.width, randY);
SKAction *moveAction = [SKAction moveTo:location duration:randDuration];
SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^(){
asteroid.hidden = YES;
}];
SKAction *moveAsteroidActionWithDone = [SKAction sequence:@[enemyCurve,moveAction, doneAction]];
感谢您的帮助和建议。
贝塞尔曲线用于生成两点之间的平滑曲线。
要从左向右移动路径,请在左侧选择起点并在右侧选择终点。这两个控制点决定了从左到右的路径形状。改变以下代码中的 startpoint
和 endpoint
将控制贝塞尔曲线的起点和终点。改变 control points
会改变曲线的形状。您可以通过附加的 GIF 看到这一点。
CGMutablePathRef cgpath = CGPathCreateMutable();
CGPoint startingPoint = CGPointMake(50, 100);
CGPoint controlPoint1 = CGPointMake(160, 250);
CGPoint controlPoint2 = CGPointMake(200, 140);
CGPoint endingPoint = CGPointMake(303, 100);
CGPathMoveToPoint(cgpath, NULL, startingPoint.x, startingPoint.y);
CGPathAddCurveToPoint(cgpath, NULL, controlPoint1.x, controlPoint1.y,
controlPoint2.x, controlPoint2.y,
endingPoint.x, endingPoint.y);
SKAction *enemyCurve = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];
[enemy runAction:[SKAction sequence:@[[SKAction waitForDuration:1],enemyCurve]]];
P0
和P3
是起点和终点,P1
和P2
是控制点。
查看此页面以更多地使用贝塞尔曲线。 http://www.jasondavies.com/animated-bezier/