我认为没有办法做到这一点,但是有没有办法检测 2 个 SKSpriteNode 何时相互交叉,但仍然允许它们重叠,所以它们实际上不会相互反弹?
我知道我可以在没有物理体的情况下只使用 1,然后编写一些代码来检查它们的坐标,但我想我可能在 Sprite Kit 中遗漏了一些我可以用 SK 方法检测到的东西。
最佳答案
您可以使用 SKPhysicsWorld
对象的 contactDelegate
属性:
// inside your header file
typedef NS_OPTIONS(NSUInteger, CollisionCategory) {
categoryOne = (1 << 0),
categoryTwo = (1 << 1)
};
// inside your SKScene sub-class implementation
- (void)setupContactDelegate {
self.physicsWorld.contactDelegate = self;
nodeA.categoryBitMask = categoryOne; // nodeA is category one
nodeA.collisionBitMask = ~categoryTwo; // nodeA does not collide w/ category two
nodeA.contactTestBitMask = categoryTwo; // nodeA tests for contacts w/ category two
nodeB.categoryBitMask = categoryTwo; // nodeB is category two
nodeB.collisionBitMask = ~categoryOne; // nodeB does not collide w/ category one
nodeB.contactTestBitMask = categoryOne; // nodeB tests for contacts w/ category one
}
- (void)didBeginContact:(SKPhysicsContact *)contact {
// do whatever you need to do when the contact begins
}
- (void)didEndContact:(SKPhysicsContact *)contact {
// do whatever you need to do when the contact ends
}
您还需要将您的 SKScene
子类声明为实现 SKPhysicsContactDelegate
协议(protocol)。
这里有更多的引用信息:
关于ios - 有没有办法检测 2 个 SKSpriteNode 之间的碰撞但允许它们重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448413/