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

spritekit view
spritekit object
spritekit scene coordinates
how does spritekit work
spritekit ui
skshapenode
skspritenode image
skspritenode copy

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")
    }
}

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
    }
}

Maximizing Node Drawing Performance, your scene by laying out nodes, and SpriteKit builds the Metal draw calls for you that actually render the content. Thus, you have little control over the amount​  Option 1 – Design using the Point Sizes for Scene Size and use 2x / 3x Images. Set the scene to the largest point size for that particular device family, for an iPhone, that would mean the size of the SKScene is set to 414 by 736. Smaller iPhones will simply scale down slightly to their point size.

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.

scene, scene. The scene node that contains this node. SDKs. iOS 7.0+; macOS 10.9+; Mac Catalyst 13.0+; tvOS 9.0+; watchOS 3.0+. Framework. SpriteKit. On This  ∞ Members will earn 250 activation bonus SCENE points if their first earn or burn transaction takes place within 90 days of joining the SCENE program. To receive the bonus points, members must present their membership card or enter their membership number at the time of transaction.

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.

Introduction to the SpriteKit Scene Editor, Creating a Scene. Many games have more than one screen or scene, so we will create a new scene from scratch and then show it from our initial  Rewards start at just 500 points for $5 off your food and drink at the movies. Find out what movies, meals and more can be yours. All Movies Dining More. Free General Admission Movie Ticket. 1,000 POINTS EACH. Get Your Ticket at Cineplex.com. $5 Off Your Food & Drink at the Movies. 500 POINTS = $5 OFF. Redeem SCENE points at Swiss Chalet!

SpriteKit Basics: Nodes, SpriteKit Introduction Part 1: Scenes, Nodes, Labels and Points Other than the position property of the label, there are a number of properties  IT'S EASY TO GET TO FREE REWARDS. Show your SCENE card to earn points and get exclusive offers on movies, meals, and more. You can redeem for FREE movies, $10 off your food and drink at the movies, or $10 off at one of 800+ restaurant locations. Rewards start at just 500 points! SEE HOW IT WORKS

SpriteKit Introduction Part 1: Scenes, Nodes, Labels and Points , Instead add a Color Sprite (which is technically an SKSpriteNode). An important aspect of Sprite Kit is that your scene can have many nodes with the same  There you can apply the SpriteKit @2x/@1x preset, which defines two scaling variants: @2x with the original sprites, and a variant without suffix containing the sprites scaled by 0.5. Additionally TexturePacker can generate a .swift file which contains a class providing useful methods for easy SKTexture creation.

Child Basics in Sprite Kit – Adding, Removing, Finding, SpriteKit is a framework that helps you create animated 2D scenes and effects. A node's position property determines the amount by which the node is moved  To add some depth the scene there are multiple SKSpriteNodes in the scene’s hierarchy. The ground represents the sea - it’s just a blue SKSpriteNode. The first Cloud layer is placed on top of the ground layer. The next layer added to the scene is the Fighter layer. This is where all of the fighters will be placed and will interact with each

Comments
  • 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
  • that is my function and i am not adding or deleting sprite nodes any where else
  • 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
  • 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...
  • This works but it only preforms the function once, even though the number of nodes becomes x more than once
  • Braces are not optional in Swift. The parenthesis which you insisted on including, however, are optional.
  • but then it is giving me the error "unresolved identifier" for the function when i call it
  • @MostafaMohsen call your function, not the one I have. function1() is just an example
  • 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
  • @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.
  • @AbdulAhmad And this answer has the Swift version, does it not?