繁体   English   中英

在 SpriteKit 中触摸移动的精灵

[英]Touching moving Sprites in SpriteKit

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

你的实体太小了吗? 根据给定的信息,我在操场上重新创建了一个小场景,一切都按预期进行。 如果我有一个呈现的子节点,定义为

let circle = SKShapeNode(circleOfRadius: 20)

我已经定义了SKScene的physicsWorld和SKNode的physicsBody,在给定的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")
       }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM