【发布时间】:2022-01-21 15:34:56
【问题描述】:
func didBegin(_ contact: SKPhysicsContact) {
// Each contact has two bodies, but we do not know which two bodies
// first we will find the player body, and then use the other body to determine the contact type
let otherBody: SKPhysicsBody
// combine the two player physics categories into one bitmask using the bitwise OR operator
let playerMask = PhysicsCategory.player.rawValue | PhysicsCategory.damagedPlayer.rawValue
// Use the bitwise AND operator to find the penguin.
// This returns a positive number if body A's category is the same as either the player or damaged player
if contact.bodyA.categoryBitMask & playerMask > 0 {
// body A is the player, so we test body B
otherBody = contact.bodyB
}
else {
// body B is the player, so we test body A
otherBody = contact.bodyA
}
// Determine the type of contact
switch otherBody.categoryBitMask {
case PhysicsCategory.ground.rawValue:
print("hit the ground")
case PhysicsCategory.enemy.rawValue:
print("hit enemy, take damage")
case PhysicsCategory.coin.rawValue:
print("collect a coin, more wealthy")
case PhysicsCategory.powerup.rawValue:
print("gained a power up")
default:
print("Contact with no game logic")
}
}
我正在尝试正确检测碰撞。每当我的播放器撞到另一个物体时,控制台就会记录多个碰撞而不是单个碰撞。所以我想知道是否有任何方法可以解决这个问题。
【问题讨论】:
标签: ios swift sprite-kit collision-detection game-physics