【问题标题】:How detect which sprites I touch如何检测我触摸了哪些精灵
【发布时间】:2017-03-31 06:52:41
【问题描述】:

我在这样的 for 循环中生成了一个砖块 (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、包含的图像等)做事

【问题讨论】:

标签: swift sprite-kit skspritenode swift-playground


【解决方案1】:

使用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'
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多