OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Sprite Kit 中创建赛道 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:28
标题: ios - 在 Sprite Kit 中创建赛道

自从 ios7 发布以来,我一直在使用 Sprite Kits 2D 游戏进行学习和创作。我现在有了创建一个完全加载的自上而下赛车游戏的想法,但我被困在一个问题上,即创建不同赛道的最佳方法是什么。最初,我想我会简单地使用 Tile Map(使用流行的程序 Tiled)创建赛道,但后来我意识到我很可能无法创建我想要的赛道的圆角。有没有人对最好的方法有什么想法?也许使用 Tile Maps "is"最好的方法,但我错过了有关处理圆角碰撞检测的关键功能..



Best Answer-推荐答案


如果您计划拥有多个级别,使用 Tiled 创建背景肯定会更容易、更高效。

目前您只能从路径创建具有矩形、圆形或多边形的物理实体。我认为创建曲线最简单和最有效的方法是使用小矩形并以相等的步长将它们倾斜。

如果您将其子类化,您可以轻松地在每个级别重用您的曲线。

在图片中,我将每个矩形从前一个矩形额外旋转了 10 度。

enter image description here

另一种选择是使用 bodyWithPolygonFromPath:SKPhysicsBody Path Generator helper tool为图像创建路径。生成的代码如下所示:

SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed"imageName.png"];

 CGFloat offsetX = sprite.frame.size.width * sprite.anchorPoint.x;
 CGFloat offsetY = sprite.frame.size.height * sprite.anchorPoint.y;

 CGMutablePathRef path = CGPathCreateMutable();

 CGPathMoveToPoint(path, NULL, 398 - offsetX, 5 - offsetY);
 CGPathAddLineToPoint(path, NULL, 334 - offsetX, 4 - offsetY);
 CGPathAddLineToPoint(path, NULL, 274 - offsetX, 18 - offsetY);
 CGPathAddLineToPoint(path, NULL, 214 - offsetX, 40 - offsetY);
 CGPathAddLineToPoint(path, NULL, 161 - offsetX, 70 - offsetY);
 CGPathAddLineToPoint(path, NULL, 112 - offsetX, 109 - offsetY);
 CGPathAddLineToPoint(path, NULL, 74 - offsetX, 161 - offsetY);
 CGPathAddLineToPoint(path, NULL, 40 - offsetX, 211 - offsetY);
 CGPathAddLineToPoint(path, NULL, 19 - offsetX, 272 - offsetY);
 CGPathAddLineToPoint(path, NULL, 10 - offsetX, 336 - offsetY);
 CGPathAddLineToPoint(path, NULL, 8 - offsetX, 394 - offsetY);
 CGPathAddLineToPoint(path, NULL, 27 - offsetX, 395 - offsetY);
 CGPathAddLineToPoint(path, NULL, 26 - offsetX, 337 - offsetY);
 CGPathAddLineToPoint(path, NULL, 37 - offsetX, 276 - offsetY);
 CGPathAddLineToPoint(path, NULL, 57 - offsetX, 220 - offsetY);
 CGPathAddLineToPoint(path, NULL, 87 - offsetX, 168 - offsetY);
 CGPathAddLineToPoint(path, NULL, 124 - offsetX, 124 - offsetY);
 CGPathAddLineToPoint(path, NULL, 169 - offsetX, 85 - offsetY);
 CGPathAddLineToPoint(path, NULL, 222 - offsetX, 55 - offsetY);
 CGPathAddLineToPoint(path, NULL, 281 - offsetX, 34 - offsetY);
 CGPathAddLineToPoint(path, NULL, 339 - offsetX, 26 - offsetY);
 CGPathAddLineToPoint(path, NULL, 395 - offsetX, 25 - offsetY);

 CGPathCloseSubpath(path);

 sprite.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];

关于ios - 在 Sprite Kit 中创建赛道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110806/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4