SKNode孩子们有一系列课程

时间:2015-02-12 13:19:54

标签: swift sprite-kit

我正在尝试编写一个方法来查找特定类的SKNode的子项 - 但我在这里得到一个EXC错误

任何有关为何会出现这种情况的想法?

import SpriteKit

extension SKNode {
  func children(#klasses: [AnyClass]) -> [SKNode] {
    var matchedNodes = [SKNode]()
    for child in children as [SKNode] {
      for klass in klasses {
        if child.isKindOfClass(klass) {
          matchedNodes.append(child)
        }
      }

      matchedNodes += child.children(klasses: klasses)
    }
    return matchedNodes
  }

}

class Apple: SKNode {}
class Orange: SKNode {}

let apple = Apple()
let orange = Orange()

apple.addChild(orange)

let fruits = apple.children(klasses: [Apple.self, Orange.self])

错误:

执行被中断,原因是:EXC_BAD_INSTRUCTION(代码= EXC_i386_INVOP,子代码= 0x0)。

1 个答案:

答案 0 :(得分:0)

试试此代码

extension SKNode {
    func children(#klasses: [AnyObject]) -> [SKNode] {
        var matchedNodes = [SKNode]()
        for child in children as [SKNode]{
            for klass in klasses {
                if child.dynamicType === klass {
                    matchedNodes.append(child)
                }
            }
            matchedNodes += child.children(klasses: klasses)
        }
        return matchedNodes
    }
}