首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在CGPath上以可变速度移动精灵

如何在CGPath上以可变速度移动精灵
EN

Stack Overflow用户
提问于 2014-08-02 19:41:49
回答 2查看 636关注 0票数 4

我正在编写一个游戏,想要沿着固定的路径(直线和曲线-想铁路引擎)移动一个精灵,但我希望能够根据精灵的速度来驱动动画-并根据游戏事件改变速度。

跟随路径:持续时间:在SKAction中是接近的,但似乎没有办法“即时”地调整速度-我绝对关心精灵的速度-而不是‘跟随路径的持续时间’。

CGPath似乎是为精灵定义路径的正确构造,但似乎没有足够的功能来获取路径上的点并进行我自己的计算。

有没有人能建议一个合适的方法?

EN

回答 2

Stack Overflow用户

发布于 2014-08-03 02:44:56

您可以使用任何SKAction的速度属性来调整其执行速度。例如,如果设置action.speed = 2,则该操作的运行速度会快两倍。

代码语言:javascript
运行
复制
SKAction *moveAlongPath = [SKAction followPath:path asOffset:NO orientToPath:YES duration:60];
[_character runAction:moveAlongPath withKey:@"moveAlongPath"];

然后可以通过以下方式调整角色的速度

代码语言:javascript
运行
复制
[self changeActionSpeedTo:2 onNode:_character];

方法来更改SKAction的速度...

代码语言:javascript
运行
复制
- (void) changeActionSpeedTo:(CGFloat)speed onNode:(SKSpriteNode *)node
{
    SKAction *action = [node actionForKey:@"moveAlongPath"];
    if (action) {
      action.speed = speed;
    }
}
票数 5
EN

Stack Overflow用户

发布于 2014-08-02 21:00:39

试着这样想:

代码语言:javascript
运行
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25094622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档