如何检测skspritekit中的碰撞节点名称?
How to detect the collision nodes name in skspritekit?
我想检测与我的播放器节点发生冲突的节点名称。这可能吗?
func didBegin(_ contact: SKPhysicsContact) {
let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
for i in CollectionObject {
if collision == Player.physicsBody!.categoryBitMask | i.physicsBody!.categoryBitMask{
print("hit the object") //here I would like to print the nodes name
}
}
}
您可以使用
contact.bodyA.node?.name
和
contact.bodyB.node?.name
获取相互碰撞的2个节点的名称。
因此,如果您的播放器节点有名称,那么您可以使用如下所示的名称:
if contact.bodyA.node?.name == "PlayerName" {
print(contact.bodyB.node?.name)
} else if contact.bodyB.node?.name == "PlayerName" {
print(contact.bodyA.node?.name)
}
我想检测与我的播放器节点发生冲突的节点名称。这可能吗?
func didBegin(_ contact: SKPhysicsContact) {
let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
for i in CollectionObject {
if collision == Player.physicsBody!.categoryBitMask | i.physicsBody!.categoryBitMask{
print("hit the object") //here I would like to print the nodes name
}
}
}
您可以使用
contact.bodyA.node?.name
和
contact.bodyB.node?.name
获取相互碰撞的2个节点的名称。
因此,如果您的播放器节点有名称,那么您可以使用如下所示的名称:
if contact.bodyA.node?.name == "PlayerName" {
print(contact.bodyB.node?.name)
} else if contact.bodyB.node?.name == "PlayerName" {
print(contact.bodyA.node?.name)
}