如何防止带有physicsBody的SKSpriteNode在与其他精灵相撞时移动?

时间:2016-12-15 06:14:16

标签: ios sprite-kit skspritenode

我有两个SKSpriteNodes,比方说一个代表一个棒球棒,另一个代表一个球。两者都有物理机构,碰撞工作正常,或至少检测到。我有一个摇摆蝙蝠的SKAction,它与球发生碰撞。当发生这种情况时,他们都开始移动。我希望球飞走,球棒留在原地。我怎样才能做到这一点?谢谢!

2 个答案:

答案 0 :(得分:3)

尝试制作bat.physicsBody.dynamic = false,或者让它不会与球碰撞(但保持球与球碰撞),即球棒的collisionBitMask不包含球categoryBitMask

bat.physicsBody?.collisionBitMask &= ~ballCategory // bat does not collide with ball

如果你希望击球击球时停球,你必须做一些诡计(例如,设置两者之间的接触,当他们接触时,移除击球上的动作和/或将击球速度设置为0)

答案 1 :(得分:0)

好吧,显然这很容易 - 在SKPhysicsBody上有一个pinned属性,我只需要将它设置为true

文件说明: A Boolean value that indicates whether the physics body’s node is pinned to its parent node. The default value is false. If true, the node’s position is fixed relative to its parent. The node’s position cannot be changed by actions or physics forces. The node can freely rotate around its position in response to collisions or other forces. If the parent node has a physics body, the two physics bodies are treated as if they are connected with a pin joint.