Created
December 27, 2017 22:02
-
-
Save yeldarby/6ace3929af2e29e20c56d0762f4f75e2 to your computer and use it in GitHub Desktop.
SpriteKit Recursive childNode withName
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just realized that you actually can search recursively with special syntax (prepending the name with
//
.For example:
childNode(withName: "//NAME HERE")