ios - 在 Sprite Kit 中创建赛道

标签 ios sprite-kit tiled jstilemap

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

最佳答案

如果您计划拥有多个级别,那么使用 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/

相关文章:

ios - 带有核心数据的预加载数据库

ios - 显示重复的 Action 导出

swift - SpriteKit 从 SKScene 回到菜单

C# 字典 - 字典中不存在给定的键

java - 在游戏中旋转整个平铺 map - LibGDX

ios - 如何在 iPad 的详细 View 中选择默认项目? (在 Split View Controller 中)

iOS In App Purchase购买多种产品

ios - 在 Spritekit 中左右倾斜宇宙飞船

ios - 使用 UIBezierPath 沿弧线移动 Sprite

java - 如何删除静态变量但仍允许全局调用变量?