swift - 如何检查一个节点是否靠近另一个节点?
问题描述
一段时间以来,我一直试图在一个节点靠近另一个节点时调用一个函数。为此,我首先处理了SKPhysicBody
,但是由于didBegin()
从未调用过该函数,因此我想问是否有人有其他想法,如何很容易地检查一个节点是否靠近另一个节点。
结构:
struct physicBodyCharacters {
static let cardNumber = 00000001 //1
static let anotherCardNumber = 00000010 //2
static let nobodyNumber = 00000100 //4
}
在 viewDidLoad() 中:
gameScene2.physicsWorld.gravity = CGVector(dx: 0, dy: -9.81)
gameScene2.physicsWorld.contactDelegate = self
第一个节点:
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: battlefieldCard0.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
}
}
我将非常感谢任何答案。
解决方案
推荐阅读
- javascript - 无法在 Markdown 中导入 Javascript 源
- kubernetes - Kubernetes 从 pod 获取入口 tls.hosts
- html - 如何从我自己的网页下载托管在 firebase 中的视频(只需单击“a”标签)?
- python - 使用 `line` 与 `multi_line` 在散景中绘制许多(不同颜色的)线
- java - 如果 Selenium 驱动程序没有找到元素(如果 - 否则如果)继续前进,但它会引发找不到元素错误
- java - 子类抛出 UnsupportedOperationException 与忽略输入参数
- python - 通过梯度下降在每一步更新的自定义损失函数
- spyder - Spyder:是否可以有两行文件选项卡?
- excel - Excel:VLOOKUP 和 FIND 组合
- java - 为什么 pyspark 会因“实例化 'org.apache.spark.sql.internal.SessionStateBuilder' 时出错”而失败?