-1

I want to find out what the number of nodes is in a scene. For example, I want to create an if statement so that if the number of nodes in the scene is 0 or any other number, I would call a function.

This is what i have done but it only calls the function the first time children.count is 0 but ignores the other times.

I am not removing or adding any sprite nodes anywhere else in my code.

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")
    }
}
nhgrif
  • 61,578
  • 25
  • 134
  • 173
Mostafa Mohsen
  • 43
  • 1
  • 12
  • if you're removing sprites at some point, show us the code of how you're doing that, otherwise we can't tell you why you're experiencing this – Abdul Ahmad Apr 20 '15 at 02:24
  • that is my function and i am not adding or deleting sprite nodes any where else – Mostafa Mohsen Apr 20 '15 at 02:49
  • 1
    If you're not removing sprites it will never be 0 again, as soon as you run the function dot, the children count becomes 1 – Abdul Ahmad Apr 20 '15 at 02:52
  • 1
    **PLEASE** don't answer your question to the point of invalidating posted answers. If your question wasn't clear enough for the posted answers to hold your hand all the way to your solution, mark one as accepted and post a new question. The posted answers answered your question perfectly last time I looked at this question... – nhgrif Apr 20 '15 at 10:29

3 Answers3

1

if you're doing this inside the scene itself you can do

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

or

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

in response to your comment:

there's an update method that runs every frame in sprite kit. Use it like this:

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if (children.count == x) {
        yourFunction() //call your function
    }
}
Abdul Ahmad
  • 9,673
  • 16
  • 64
  • 127
  • This works but it only preforms the function once, even though the number of nodes becomes x more than once – Mostafa Mohsen Apr 20 '15 at 01:49
  • Braces are not optional in Swift. The parenthesis which you insisted on including, however, are optional. – nhgrif Apr 20 '15 at 01:56
  • but then it is giving me the error "unresolved identifier" for the function when i call it – Mostafa Mohsen Apr 20 '15 at 01:57
  • @MostafaMohsen call your function, not the one I have. `function1()` is just an example – Abdul Ahmad Apr 20 '15 at 01:58
  • okay, it works but it seems for me putting it in the update method only calls the function the first time that x is 0, and then it doesn't call it the next time x is 0 – Mostafa Mohsen Apr 20 '15 at 02:14
  • how are you removing the sprites? are you calling `removeFromParent()` or just hiding them? if you just hide it, it might still be part of the "children" array – Abdul Ahmad Apr 20 '15 at 02:15
1

use this:

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

where some number is a trigger number or expression.

Objective-C, use:

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

Also, where it says "enter code here," call your function and do what you need to do.

DDPWNAGE
  • 1,423
  • 10
  • 37
  • 2
    @AbdulAhmad He provided both options. And honestly, given that there's no code in the question itself, I don't think it's really fair to call this a "Swift" question. – nhgrif Apr 20 '15 at 02:02
  • @AbdulAhmad And this answer has the Swift version, does it not? – nhgrif Apr 20 '15 at 02:03
0

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

Use this method:

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

This will give you the count of all nodes in the entire scene heirarchy.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320