swift - 区分单个 Sprite 上的多个物理体

标签 swift sprite-kit skphysicsbody

假设我有三个 Sprite ;一个玩家、一个三角形和一个正方形。

三角形和方形节点各有一个SKPhysicsBody,而玩家节点有两个 - 内部和外部,大小不同。

内部仅用于记录与三角形的接触,而外部则为正方形。

我已将每个物理体分配给玩家节点:

playerNode.physicsBody = SKPhysicsBody(bodies: [inner, outer])

并给每个人一个名字:

inner.node?.name = "inner"
outer.node?.name = "outer"

但是,在 didBegin 中的任一联系人上,打印:

contact.bodyB.node?.physicsBody?.node?.name 

显示 nil 或玩家节点的名称(如果有的话)。这与打印不同:

contact.bodyB.node?.name

这是给玩家节点本身的。如果一个物理体有自己的名字,为什么我不能识别它以区别于另一个物理体?

最佳答案

这是因为你还没有真正完成你想做的事情:

The triangle and the square nodes each have a SKPhysicsBody assigned while the player node has two - an inner and an outer, of different sizes.

当您尝试使用此行将 2 个物理体分配给玩家节点时:

playerNode.physicsBody = SKPhysicsBody(bodies: [inner, outer])

你实际上做的是创建一个新的物理体(它是物理体 innerouter 的联合)并分配这个新的物理体(它有没有名字)并将其分配给您的播放器节点。

https://developer.apple.com/documentation/spritekit/skphysicsbody/1519736-init

Creates a physics body by performing a union of a group of volume-based physics bodies.

所以玩家仍然只有一个物理体(既不是内部也不是外部),这是一个节点所能拥有的。

实现此功能的一种方法是创建 2 个新节点 - playerInnerplayerOuter 也许每个节点都有自己的物理体 - 并加入它们以创建玩家节点. (playerInner 可以是 playerOuter 的子节点)

关于swift - 区分单个 Sprite 上的多个物理体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53588811/

相关文章:

ios - 没有订阅/绑定(bind)/驱动的 RxSwift 变量组合

swift - 在 NSError 初始值设定项中为代码传递枚举时出错

ios - 可与 SpriteKit 的 labelNodeWithFontNamed 方法一起使用的字体列表

Swift SpriteKit 基本接触/碰撞

swift - SKPhysicsBody( body : collisionBitMask does not work

ios - 增加 UIPageControl 指示点之间的间距

ios - 结构中的 Swift Init 可选数组

ios - SKPhysicsJointLimit 具有弹性行为,但我需要强大的绳索模拟

swift - 如何让 SKSprite 节点在游戏开始时自行左右移动?

ios - SKPhysicsBody 减速程序