Search code examples
iosswiftsknode

for(SKNode * node in nodes) in swift


Am new to swift and learning I use " for(SKNode * node in nodes) " for selecting the particular node with its name in objective-C but in swift I need some help to do this. Thanks in advance


Solution

  • override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
                let touch = touches.anyObject() as UITouch
                let touchLocation = touch.locationInNode(self)
                let nodes = self.nodesAtPoint(touchLocation) as [SKNode]
    
                for node in nodes {
                    if let nodeName = node.name {
                        if nodeName == "myNodeName" {
                            println("node tapped")
                        }
                    }
                }
            }
    

    To check if a node is tapped, iterate with for loop. Loop through the SKNodes, and check if the node name matches. Difference is that instead of (SkNode* node) we have :

    let nodes = self.nodesAtPoint(touchLocation) as [SKNode]