Reputation: 20835
I'm attempting to write a SpriteKit game but am quickly running into issues. As you can see below I'm drawing ~350 simple nodes on the screen. This produces ~25fps on my iPhone 6+.
Basically I'm just creating Section
objects (which is a SKNode), inside each of those I am looping 20 times and adding the individual blocks. The blocks are slowly moved up the screen. I'm not exactly sure where to start debugging this or which part of the code to show so here's an excerpt from the Section class that append the blocks:
for i in 1...20 {
let left = SKShapeNode(rectOfSize: CGSize(width: n, height: n))
left.fillColor = randomColor()
left.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: n - 1, height: n - 1))
left.physicsBody?.dynamic = false
left.position.x = (CGFloat(i) * n) - (size.width/2 + n/2)
row.addChild(left)
cubes.append(left)
}
Upvotes: 1
Views: 378
Reputation: 581
LearnCocos2D is correct, Your problem isn't the number of nodes it's the number of draw calls.
In your case you don't even need textures, you can create SKSpriteNodes from just a colour and a size. Only thing is you'll lose the white stroke.
You might be able to drop this right in:
for i in 1...20 {
let left = SKSpriteNode(color: randdomColor(), size: CGSize(width: n, height: n))
left.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: n - 1, height: n - 1))
left.physicsBody?.dynamic = false
left.position.x = (CGFloat(i) * n) - (size.width/2 + n/2)
row.addChild(left)
cubes.append(left)
}
Upvotes: 0
Reputation: 64477
Your nodes are all skshapenodes. Each shape node generates a new draw call, that's why you have 368 draws in that scene - which is a lot even on the latest phone.
It is my opinion that SkShapeNode was never intended as a game-building node but rather one you can visualize debugging info with. One should not build a game largely based or dependent on shape nodes simply because it is highly inefficient.
Instead, use sprites with actual textures, you'll see much better performance. If the screenshot is representative of the game's graphics you only need a white, square image as texture, then change individual sprite's color properties to tint them.
Upvotes: 8