ios - 在 SpriteKit 中触摸移动的 Sprite

标签 ios swift sprite-kit

我正在创建一个涉及触摸下落目标的 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/

相关文章:

ios - Webview 不刷新图像

ios - 通过 Facebook 登录确认传递 View Controller

swift - 初始化后如何更改实例变量?

ios - ImageView 和 Buttons 在 sprite 套件中显示/隐藏

ios - 如何根据下载的图像大小创建动态大小的行高?

ios - 如何在 iOS 中设置表格 View 的上边距?

ios - SceneKit – NSAttributedString 未显示为 SCNText

ios - SKAction.repeatActionForever 只在第一次调用时计算随机数

sprite-kit - 有没有办法在 SpriteKit 中创建凹面物理体?

ios - iOS 中 UIImageView 的动画圆形 mask