Search code examples
swiftsprite-kitsknode

SpriteKit childNodeWithName can't find existing node


I have this code in SKScene:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {

    var touch: AnyObject = touches.anyObject()
    var point = getPoint(touch.locationInNode(self))
    var name  = NSStringFromCGPoint(point)

    for children in self.children {

        if (children as SKSpriteNode).name == name {

            println("exist!")
        }
    }
    var tempNode = self.childNodeWithName(name)
}

I see "exist!" in log, so there is a node with this name in children array, but tempNode is nil. The self.childNodeWithName("//" + name)call returns nil too.


Solution

  • Here is how to accomplish this in Swift... Hope this helps!

    var mySprite: SKSpriteNode = childNodeWithName("mySprite") as SKSpriteNode