处理联系人
将场景设置为委托
//set your scene as SKPhysicsContactDelegate
class yourScene: SKScene, SKPhysicsContactDelegate
self.physicsWorld.contactDelegate = self;
然后你必须实现一个或另一个联系人函数:可选的 func didBegin(联系人)和/或可选的基金 didEnd(contact :)方法来填写你的联系逻辑,例如像
//order
let bodies = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? (A:contact.bodyA,B:contact.bodyB) : (A:contact.bodyB,B:contact.bodyA)
//real handler
if ((bodies.B.categoryBitMask & boxBody) == boxBody){
if ((bodies.A.categoryBitMask & groundBody) == groundBody) {
let vector = bodies.B.velocity
bodies.B.velocity = CGVectorMake(vector.dx, vector.dy * 4)
}else{
let vector = bodies.A.velocity
bodies.A.velocity = CGVectorMake(vector.dx, vector.dy * 10)
}
}