我想测试一个节点何时触摸另一节点。为此,我使用以下代码:
struct physicBodyCharacters {
static let cardNumber = 1
static let anotherCardNumber = 2
static let nobodyNumber = 0
}
第一个节点:
card = SKSpriteNode(texture: cardTexture)
card.position = CGPoint(x: gameScene2.size.width / 2 + 150, y: 95)
card.zPosition = 3
card.setScale(1)
card.physicsBody = SKPhysicsBody(texture: cardTexture, size: card.size)
card.physicsBody?.affectedByGravity = false
card.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.cardNumber)
card.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
card.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.anotherCardNumber)
第二个节点:
anotherCard = SKSpriteNode(texture: anotherCardTexture)
anotherCard.position = CGPoint(x: 31 , y: 532)
anotherCard.zPosition = 2
anotherCard.setScale(1)
anotherCard.physicsBody = SKPhysicsBody(texture: anotherCardTexture, size: anotherCard.size)
anotherCard.physicsBody?.affectedByGravity = false
anotherCard.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.anotherCardNumber)
anotherCard.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
anotherCard.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.cardNumber)
didBegin函数:
func didBegin(_ contact: SKPhysicsContact) {
print("contact")
let contanctMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contanctMask
{
case UInt32(physicBodyCharacters.cardNumber) | UInt32(physicBodyCharacters.anotherCardNumber):
print("hit")
default:
break
}
}
我的目标是输出“命中” ,但不更改card
和anotherCard
的位置。他们应该彼此碰触,但不能移动。
提前谢谢