我正在使用 Swift 和 SpriteKit
我有一个类:
class Button: SKShapeNode
{
}
我有一些按钮:
var button1 = Button()
var button2 = Button()
我已经使用了那些节点的“.name”:
button1.name = "button1"
button2.name = "button2"
所以通常情况下,我用 enumerateChildNodesWithName("button")
枚举这些节点,但在这里,名称已经被占用,所以我如何枚举我的所有按钮(使用 Button 类)?
最佳答案
您可以通过以//*
作为节点名称调用enumerateChildNodesWithName
来枚举给定节点的所有子节点。这在 Apple 文档中作为示例列出 here .
然后在 block 中检查节点是否属于 Button
类型并相应地执行您的操作。
所以像这样:
myNode.enumerateChildNodesWithName("//*") { node, ptr in
if node is Button {
// do something here
}
}
关于swift - 在 Swift 中枚举类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38810886/