我正在像这样的 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/