ios - 如何在 SpriteKit 中为带有物理体的迷宫创建路径

标签 ios macos sprite-kit skphysicsbody cgpath

我想为我的 SpriteKit 游戏创建一个迷宫,但我在设置正确的物理体时遇到了问题。看看这个例子:

example

蓝色:CGPath

绿色:迷宫(CGPath 线宽=10)

红色:我要创建的物理体

我尝试用 CGPath 和 SKShapeNode 绘制迷宫,但如何为迷宫的内部边界设置物理体?

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 38, 266);
CGPathAddLineToPoint(path, NULL, 37, 23);
CGPathAddLineToPoint(path, NULL, 127, 22);
CGPathAddLineToPoint(path, NULL, 127, 184);
CGPathAddLineToPoint(path, NULL, 273, 185);
CGPathAddLineToPoint(path, NULL, 273, 98);
CGPathAddLineToPoint(path, NULL, 274, 184);
CGPathAddLineToPoint(path, NULL, 127, 184);
CGPathAddLineToPoint(path, NULL, 127, 23);
CGPathAddLineToPoint(path, NULL, 364, 23);
CGPathAddLineToPoint(path, NULL, 364, 266);
CGPathCloseSubpath(path);
SKShapeNode *labyrinth = [SKShapeNode shapeNodeWithPath:path];
labyrinth = [SKColor greenColor];
labyrinth.lineWidth = 10;
[self addChild:labyrinth];

.

labyrinth.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:path];

不起作用。我需要一个选项来包括我的物理体路径的线宽。

对于实现这样的迷宫有什么建议或其他选择吗?

最佳答案

您是否考虑过用矩形 build 迷宫?

每个迷宫墙都可以是矩形或 Sprite 节点,然后您将使用 bodyWithRectangleOfSize:为每个形状/ Sprite 节点提供一个与其自身大小相同的物理体。

关于ios - 如何在 SpriteKit 中为带有物理体的迷宫创建路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31698615/

相关文章:

应用程序关闭时的 IOS 通知

iphone - 如何在 NSMutableArray 里面订购 NSMutableArray

macos - 获取脚本中 OS X shell 脚本所在的路径...当路径有空格时

ios - Fatal error : init(coder:) has not been implemented. 运行时哪里出错

ios - 如何使用 spriteKit 快速绘制圆圈?

swift - 将 SKLabelNode 子类化到当前场景,怎么样?

ios - 保存高分

android - cordova push 和本地通知冲突

objective-c - sqrtf(6)*sqrtf(6) != 6 在 xcode 中

c - openSSL mac undefined symbol