在Sprite Kit中创建赛道

时间:2014-04-16 13:23:25

标签: ios sprite-kit tiled jstilemap

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

1 个答案:

答案 0 :(得分:2)

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

目前,您只能使用路径中的矩形,圆形或多边形创建物理实体。我认为创建曲线的最简单和最有效的方法是使用小的rects并以相等的步长角度。

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

在图片中,我将每个矩形旋转了前一个矩形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];