Swift 如何在SpriteKit中检测两个对象何时接触
我在互联网上找不到关于如何做到这一点的信息。我只是想在触摸身体时运行一行代码。在这个例子中,我有一个带物理体的SKSpriteNode,另一个是地面。当他们触摸它时,它应该运行一行代码,这是我到目前为止所发现的全部Swift 如何在SpriteKit中检测两个对象何时接触,swift,sprite-kit,Swift,Sprite Kit,我在互联网上找不到关于如何做到这一点的信息。我只是想在触摸身体时运行一行代码。在这个例子中,我有一个带物理体的SKSpriteNode,另一个是地面。当他们触摸它时,它应该运行一行代码,这是我到目前为止所发现的全部 let catGroup:UInt32 = 0x1 << 0 let groundGroup:UInt32 = 0x2 << 1 cat.physicsBody?.categoryBitMask = catGroup cat.physi
let catGroup:UInt32 = 0x1 << 0
let groundGroup:UInt32 = 0x2 << 1
cat.physicsBody?.categoryBitMask = catGroup
cat.physicsBody?.contactTestBitMask = groundGroup
ground.physicsBody?.categoryBitMask = groundGroup
ground.physicsBody?.contactTestBitMask = catGroup
让catGroup:UInt32=0x1不要成为一个固执己见的人,但你不应该以“我在互联网上找不到任何东西”开始提问,因为这显然是不正确的。在SpriteKit中,碰撞检测是基础知识之一,有上百万的教程
现在谈谈你的问题。你没有给你的精灵一个真实的物理身体,你的物理类别设置得很奇怪。将您的代码更改为此
struct PhysicsCategory {
static let cat:UInt32 = 0x1 << 0
static let ground:UInt32 = 0x1 << 1
}
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
physicsWorld.contactDelegate = self
cat.physicsBody = SKPhysicsBody(rectangleOfSize: cat.size) // FORGOT THIS
cat.physicsBody?.categoryBitMask = PhysicsCategory.cat
cat.physicsBody?.contactTestBitMask = PhysicsCategory.ground
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size) // FORGOT THIS
ground.physicsBody?.categoryBitMask = PhysicsCategory.ground
ground.physicsBody?.contactTestBitMask = PhysicsCategory.cat // You dont really need this line as long as you have set it on the other body.
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == PhysicsCategory.cat && secondBody.categoryBitMask == PhysicsCategory.ground {
print("contact")
}
}
}
希望这有助于可能的重复您是否设置了didBeginContact必须与Physivs联系的类委托协议,以及设置了“Delegate”属性?完成此操作后,在dudBeginContact中放入一个打印(“检测到联系人”)以查看是否正在调用它(或者更恰当地说,设置一个断点)。您还不能替换firstBody和secondBody,因为您需要检查它们的类别BitMask,以确定哪只是猫,哪只是地面。感谢您的帮助,但它尝试了此操作,但没有成功。很抱歉,物理类别结构中有1个小错误,应该是静态的。我更新了我的全部答案,使它更容易阅读。让我知道进展如何
struct PhysicsCategory {
static let cat:UInt32 = 0x1 << 0
static let ground:UInt32 = 0x1 << 1
}
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
physicsWorld.contactDelegate = self
cat.physicsBody = SKPhysicsBody(rectangleOfSize: cat.size) // FORGOT THIS
cat.physicsBody?.categoryBitMask = PhysicsCategory.cat
cat.physicsBody?.contactTestBitMask = PhysicsCategory.ground
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size) // FORGOT THIS
ground.physicsBody?.categoryBitMask = PhysicsCategory.ground
ground.physicsBody?.contactTestBitMask = PhysicsCategory.cat // You dont really need this line as long as you have set it on the other body.
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == PhysicsCategory.cat && secondBody.categoryBitMask == PhysicsCategory.ground {
print("contact")
}
}
}
cat.physicsBody?.affectedByGravity = false
ground.physicsBody?.affectedByGravity = false