如何获取场景中 skspritenode 的数量?

How can I get the number of skspritenodes in a scene?

我想知道一个场景中的节点数是多少。例如,我想创建一个 if 语句,以便如果场景中的节点数为 0 或任何其他数字,我将调用一个函数。

这就是我所做的,但它只在第一次调用该函数children.count 为 0,但忽略其他时间。

我不会在我的代码中的其他任何地方删除或添加任何精灵节点。

func dot(){
    var dotTexture = SKTexture (imageNamed: "dot")
    dotTexture.filteringMode = SKTextureFilteringMode.Nearest

    var dot = SKSpriteNode(texture: dotTexture)
    dot.setScale(0.5)
    dot.position = CGPoint(x: self.frame.size.width * 0.5 , y: self.frame.size.height * 1.1)

    dot.physicsBody = SKPhysicsBody (circleOfRadius: dot.size.height/2.0)
    dot.physicsBody?.dynamic = true
    dot.physicsBody?.allowsRotation = false

    self.addChild(dot)


    println("done")

}

 override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if children.count == 0 {
        dot()

        println("dot")
    }
}

如果您在场景本身内执行此操作,您可以执行此操作

if (self.children.count == x) {
   yourFunction() //call your function
}

if (children.count == x) {
    yourFunction() //call your function
}

回应您的评论:

sprite kit 中有一个运行每一帧的更新方法。像这样使用它:

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if (children.count == x) {
        yourFunction() //call your function
    }
}

使用这个:

if (self.children.count == someNumber) {
    <#enter code here#>
}

其中一些数字是触发数字或表达式。

Objective-C,使用:

if ([self children].count == someNumber) {
    // enter code here
}

此外,它说 "enter code here," 调用您的函数并执行您需要执行的操作。

I want to find out what the number of nodes is in a scene

使用这个方法:

func nodeCount(scene:SKScene) -> Int {
    return scene[".//*"].count
}

这将为您提供整个场景层次结构中所有节点的计数。