我需要说的是,一位开发人员告诉您应该创建一个 skphysicals 主体,以便该函数主体能够正常工作,那么是否有任何特定代码来创建该 Sprite 的 skphysicals 主体?因为我已经在物理部分的 sks 文件中给出了 alpha 掩码。顺便说一句,他还给了我一个代码示例:
if let _ = physicsworld.body(at : fingerlocation)
但是你知道,当我在 touchesmoved
函数中使用此代码时,它不仅仅适用于该 Sprite 的屏蔽部分,它适用于该 Sprite 的整个边界矩形
最佳答案
如果我理解正确的话,您希望将物理体限制为 Sprite 的纹理而不是矩形。
let texture = SKTexture(imageNamed: "imageNameHere")
maskedSprite.physicsBody = SKPhysicsBody(texture: texture , size: texture.size)
如果你只是想看看它是否发生了接触,你根本不需要物理体,你可以为节点添加一个名称,然后在 TouchesBegan 中检查触摸点处的节点,看看它是否已经发生了接触。与蒙面 Sprite 的名称相同。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
switch atPoint(location).name {
case "maskedsprite"?: print("masked sprite touched")
default: break
}
}
}
关于swift - 如何检测您的手指是否与蒙面 Sprite 接触,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46160742/