How to Count Number of Sprites Swift

how to count number of sprites swift

From iOS8, SKNode has subscript member that queries nodes and returns Array<SKNode>.

extension SKNode {
subscript (name: String) -> [SKNode] { get }
}

So you can:

let count = self["box"].count
println(count)

instead of:

var counter = 0
self.enumerateChildNodesWithName("box") { _, _ in
counter += 1
}
println(counter)

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

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

How do I count the number of shown sprites?

Give sprite node a name like this:

var mySprite = SkSpriteNode(imageNamed: "nameOfYourImage")
mySprite.name = "spriteToTrack"

Then enumerate throught sprites with name "spriteToTrack" like this:

var countSprites = 0
enumerateChildNodesWithName(""spriteToTrack""){node,_ in
countSprites++
/* If you want to manipulate this node or remove it....but dont do it in this loop!
let tmpNode = node as! SKSpriteNode // Must be the same as mySprite
*/
}

println("I have \(countSprites ) sprites with name spriteToTrack")

Swift Spritekit count touches

You need a member variable that tracks the number of touches since your app started up. The method touches.count isn't cumulative.

var cumulativeNumberOfTouches = 0

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)

if node == myNode {

cumulativeNumberOfTouches += 1

switch cumulativeNumberOfTouches {
case 1:
action1()
case 2:
action2()
case 3:
action3()
default:
/* do something or nothing or whatever */
println("\(cumulativeNumberOfTouches) touches")
}
}
}

}

Getting an exact node count and performance issues in Swift/SpriteKit

You can also create an extension of SKNode to calculate all the nodes in the subtree starting from the current node.

extension SKNode {
func subtreeCount() -> Int {
return children.reduce(1) { $0 + $1.subtreeCount() }
}
}

Now inside your scene simply write

let totalNodes = subtreeCount()

How do I get the node count of SKSpriteNodes not visible in the scene?

Because showsNodeCount only shows the on-screen nodes, you'd have to create your own SKSpriteNode class and create a method like addMyChild. And whenever you call the addMyChild method you increase a counter by one so that you manually know, how many nodes exist. for example:

func addMyChild(node:SKSpriteNode){
self.addChild(node)
nodeCount++
}

SpriteKit Swift Node Count issues

This is happening because SKShapeNode fillColor is set. Obviously, when either fillColor or strokeColor are set, each of them require additional node and additional draw pass . By default there is already strokeColor set (one node, one draw pass) and because of fillColor is set additionally, two nodes (and two drawing passes) are required for shape to be rendered.

SKShapeNode is not a performant solution in many cases, because of obvious reasons. One SKShapeNode requires at least one draw call. There are some interesting post related to this topic, for example this one or this one.

But still IMO, SKShapeNode should be avoided if it's not the only solution. Just because of performance reasons. And in your case, if you for example want to have many different kind of balls, the performant solution would be to use texture atlas and SKSpriteNode for representing those textures. This way you can render as much balls as you like in single draw pass.

Note that draw passes are much more important than number of nodes when it comes to performance. SpriteKit can render hundreds of nodes at 60fps if amount of drawing passes required for scene rendering is low.

How to keep node count of invaders in space game stable in xcode spritekit with swift?

Particularly i think that it's not a good idea to put this code on the update func as it will run your code before render every frame...so it will run it like 60 times per second.

A better solution is create a Function to spawn enemies and call it with a timer. First of all declare a variable in your class to handle the enemy count and the timer:

var enemies = 0
var enemyTimer = Timer()

func spawn(){
//YOUR SPAWN CODE
enemies += 1 //Will increase 1 enemy to the enemies variable every time this function is called
}

to start the timer:

 enemyTimer = Timer.scheduledTimer(timeInterval: YOUR TIME INTERVAL TO SPAWN ENEMIES, target: self, selector: #selector(YOUR_CLASS_NAME.spawn), userInfo: nil, repeats: true)

Start the timer when the game starts, invalidate it when the game ends and when you move to another scene!
This timer will call the spawn func every "n" seconds!

About the out of screen you may use the update func:

      if scene?.frame.contains(enemyNode.position){
//nothing here
}else{
//Out of screen, do stuff here...
enemyNode.removeFromParent()
}

Sprite appearing in node count but not visible

I assume that the background node size is just the same as the image size. I also assume that the scene size is the same as the image size. If that's the case, try the following.

let backTexture = SKTexture(imageNamed: "background")
let background = SKSpriteNode(texture: backTexture)
//background.size = CGSize.init(width: (self.scene?.size.width)!, height: (self.scene?.size.height)!)
//background.anchorPoint = self.anchorPoint
background.position = CGPoint(x: CGFloat(i) * background.size.width, y: 0))


Related Topics



Leave a reply



Submit