我想要一个精灵在触摸另一个精灵时删除自己.就在他们触摸时,他们只是互相推动.
我有这个:
let alphaCategory: UInt32 = 0x1 << 0 let betaCategory: UInt32 = 0x1 << 1
我让精灵充满活力而不受重力的影响
self.physicsworld.contactDelegate = self alpha.physicsBody?.categoryBitMask = alphaCategory alpha.physicsBody?.contactTestBitmask = betaCategory
和
beta.physicsBody?.categoryBitMask = betaCategory beta.physicsBody?.contactTestBitmask = alphaCategory
我找不到任何对我有意义的东西,但我认为问题就在这里
func didBeginContact(contact: SKPhysicsContact) { var firstBody: SKPhysicsBody var secondBody: SKPhysicsBody if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { firstBody = contact.bodyA secondBody = contact.BodyB beta.removeFromParent() } }
首先,你应该设置你的firstBody& secondBody到他们的collisionBitMask的顺序:
原文链接:https://www.f2er.com/swift/320101.htmlif contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { firstBody = contact.bodyA secondBody = contact.bodyB } else { firstBody = contact.bodyB secondBody = contact.bodyA } if firstBody.categoryBitMask=0 && secondBody.categoryBitMask=1 { secondBody.removeFromParent() }
这将防止你的精灵与任何东西(包括彼此)发生碰撞.将其设置为设置其他BitMask属性的位置:
alpha.physicsBody.collisionBitMask = 0 beta.physicsBody.collisionBitMask = 0