swift - 如何检测我触摸了哪些 Sprite

标签 swift sprite-kit skspritenode swift-playground

我正在像这样的 for 循环中生成砖 block 网格 (SKSpriteNode):

for row in 0..<numRows
    {
        for col in 0..<numColumns
        {
            let brick = SKSpriteNode(color: UIColor.white, size: CGSize(width: brickWidth, height: brickHeight))

            brick.anchorPoint = CGPoint.zero
            brick.name = "brick_d" 
            brick.position = CGPoint(x: brickFirstXPosition + col * 65, y: brickFirstYPosition + row * 65)

            self.addChild(brick)
        }
    }

现在我不知道如何区分一个节点和另一个节点,我必须根据它们的不同位置或其他什么来做这个? 实际上,我需要使用 touchesBegan 函数来触摸一个特定节点。用于根据节点属性(bg、包含的图像等)做事

最佳答案

使用touchesBegan():

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        let touch = touches.first // get the first touch
        let touchedNode = selectNodeForTouch(touch.location(in: self))
        // do stuff with your 'touchedNode'
    }

关于swift - 如何检测我触摸了哪些 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134286/

相关文章:

objective-c - 我的角色Sprite Node在移动时晃动,为什么?

swift - self.name = Swift 中的名称。我不明白为什么需要这些代码

ios - UILongPressGestureRecognizer 中断 tableView 滚动

ios - 使用 dispatch_async 在后台加载图像

ios - 如何根据 iDevice 精确测量 Swift 节点的大小和位置

ios - 以圆形模式向外移动 SpriteNode

ios - SKPhysicsJointLimit 具有弹性行为,但我需要强大的绳索模拟

ios SWIFT - 如何在 UITextField 上启用水平滚动并在文本长于字段宽度时禁用截断 "..."

swift - 为什么 Realm 建议使用 "let"声明 List<T> 属性?

ios - 如何在 SpriteKit 中添加高亮按钮?