Skip to content

Instantly share code, notes, and snippets.

@yeldarby
Created December 27, 2017 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeldarby/6ace3929af2e29e20c56d0762f4f75e2 to your computer and use it in GitHub Desktop.
Save yeldarby/6ace3929af2e29e20c56d0762f4f75e2 to your computer and use it in GitHub Desktop.
SpriteKit Recursive childNode withName
/*
Copyright (c) 2017 Brad Dwyer. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT>.
SceneKit contains a recursive search for childNodes but SpriteKit inexplicably doesn't.
So here's an extension that adds one, enjoy.
*/
extension SKNode {
func childNode(withName name: String, recursively: Bool) -> SKNode? {
let directDescendent = childNode(withName: name)
if(directDescendent != nil) { return directDescendent }
for c in children {
let child = c.childNode(withName: name, recursively: true)
if(child != nil) { return child }
}
return nil
}
}
@yeldarby
Copy link
Author

I just realized that you actually can search recursively with special syntax (prepending the name with //.

For example: childNode(withName: "//NAME HERE")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment