我正在创建一个涉及触摸下落目标的 SpriteKit 游戏。目前,第一次触摸时很难捕捉到目标 (touchesBegan:
),并且似乎只能通过提前放置手指来触摸 (touchesMoved:
)。是否有一种技术可以抑制触摸或加宽触摸位置以使第一次触摸更有效?我的代码现在看起来像这样:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let positionInScene = touch.location(in: self)
print(positionInScene)
guard let touchedNode = self.nodes(at: positionInScene).first as? SKSpriteNode else { return }
if let dot = touchedNode.name {
if dot == "dot" {
removeTarget(touchedNode)
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let positionInScene = touch.location(in: self)
print(positionInScene)
guard let touchedNode = self.nodes(at: positionInScene).first as? SKSpriteNode else { return }
if let dot = touchedNode.name {
if dot == "dot" {
removeTarget(touchedNode)
}
}
}
最佳答案
您的实体太小了吗?根据给定的信息,我在 Playground 上重新创建了一个小场景,一切都按预期进行。如果我有一个呈现的子节点,定义为
let circle = SKShapeNode(circleOfRadius: 20)
并且我已经定义了SKScene的physicalWorld和SKNode的physicalBody,使用给定的touchesBegan,检测碰撞没有问题。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let location = touches.first?.location(in: self)
if self.atPoint(location) === circle {
print("TOUCH")
}
}
}
关于ios - 在 SpriteKit 中触摸移动的 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56739925/