2

I have been learning and creating using Sprite Kits 2D games since its release with ios7. I now have an idea of creating a fully loaded top down racing game but I am stuck on the single question as to what is the best approach to creating the different race tracks. Initially, I figured I would simply create the race track with a Tile Map (using popular program Tiled) but then I realized that I most likely would not be able to create the rounded corners of the track that I wanted. Does anyone have any thoughts on what the best approach for this would be? Perhaps using Tile Maps "is" the best approach but I am missing out on a key function regarding handling collision detection on the rounded corners..

4

1 回答 1

2

Creating your background with Tiled is certainly going to be easier and more efficient if you are planning on having multiple levels.

Currently you can only create a physics body with either a rect, circle or polygon from path. I think the easiest and most efficient way to create curves is to use small rects and angle them in equal steps.

If you subclass this you can easily reuse your curves in every level.

In the picture I rotate each rect by an additional 10 degrees from the previous one.

enter image description here

Another option is to use bodyWithPolygonFromPath: and the SKPhysicsBody Path Generator helper tool to create a path for the image. The resulting code would look something like this:

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];
于 2014-04-16T14:35:07.287 回答