我正在编写一个游戏,想要沿着固定的路径(直线和曲线-想铁路引擎)移动一个精灵,但我希望能够根据精灵的速度来驱动动画-并根据游戏事件改变速度。
跟随路径:持续时间:在SKAction中是接近的,但似乎没有办法“即时”地调整速度-我绝对关心精灵的速度-而不是‘跟随路径的持续时间’。
CGPath似乎是为精灵定义路径的正确构造,但似乎没有足够的功能来获取路径上的点并进行我自己的计算。
有没有人能建议一个合适的方法?
发布于 2014-08-03 02:44:56
您可以使用任何SKAction的速度属性来调整其执行速度。例如,如果设置action.speed = 2,则该操作的运行速度会快两倍。
SKAction *moveAlongPath = [SKAction followPath:path asOffset:NO orientToPath:YES duration:60];
[_character runAction:moveAlongPath withKey:@"moveAlongPath"];然后可以通过以下方式调整角色的速度
[self changeActionSpeedTo:2 onNode:_character];方法来更改SKAction的速度...
- (void) changeActionSpeedTo:(CGFloat)speed onNode:(SKSpriteNode *)node
{
SKAction *action = [node actionForKey:@"moveAlongPath"];
if (action) {
action.speed = speed;
}
}发布于 2014-08-02 21:00:39
试着这样想:
CGMutablePathRef cgpath = CGPathCreateMutable();
//random values
float xStart = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float xEnd = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
//ControlPoint1
float cp1X = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float cp1Y = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.height ];
//ControlPoint2
float cp2X = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float cp2Y = [self getRandomNumberBetween:0 to:cp1Y];
CGPoint s = CGPointMake(xStart, 1024.0);
CGPoint e = CGPointMake(xEnd, -100.0);
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 *planeDestroy = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];
[self addChild:enemy];
SKAction *remove2 = [SKAction removeFromParent];
[enemy runAction:[SKAction sequence:@[planeDestroy,remove2]]];
CGPathRelease(cgpath);https://stackoverflow.com/questions/25094622
复制相似问题