I'm trying to create a scene with SpriteKit which has thousands of sprites (~500 - 2000). Each sprite is just a white pixel 1x1 - there's no need to even use textures for them.
Adding this much sprites to the scene directly at once is impossible (or at least i think so). On iPhone 6 I end up with ~200 sprites added, then system ends the adding process because of memory and the rest of the sprites aren't added.
I have found a clever solution to this called Bit Blitting where all the sprites are added to a node, which is then "converted" to texture with textureFromNode:
method and from this texture is then created a single sprite which will be finally added to a screen. It works great and I'm able to create more than 10 000 sprites at once with great fps.
My problem is that I can't move these sprites afterwards (= change position). The texture always remains the same no matter what i do. What am I missing?
My Code:
override func didMoveToView(view: SKView) {
self.generateRandomSprites()
}
func generateRandomSprites() {
let texture = SKTexture(imageNamed: "whitePixel")
self.canvasNode = SKNode()
log("started generating all sprites")
for var i = 0;i < 1000;i++ {
let width = self.scene!.frame.size.width
let height = self.scene!.frame.size.height
let x: CGFloat = CGFloat(arc4random_uniform(UInt32(width)))
let y: CGFloat = CGFloat(arc4random_uniform(UInt32(height)))
let cell = SKSpriteNode(texture: texture)
cell.position = CGPointMake(x, y)
self.arrCells.append(cell)
self.canvasNode.addChild(cell)
}
self.canvasTexture = self.view!.textureFromNode(self.canvasNode)
self.canvasSprite = SKSpriteNode(texture: self.canvasTexture, size: self.frame.size)
self.canvasSprite.anchorPoint = ccp(0,0)
self.addChild(self.canvasSprite)
}
override func update(currentTime: CFTimeInterval) {
for oneCell in self.arrCells {
oneCell.position = CGPointMake(oneCell.position.x + 1, oneCell.position.y)
}
self.canvasTexture = self.view!.textureFromNode(self.canvasNode)
self.canvasSprite.texture = self.canvasTexture
}
Screenshot from app (this is static, nothing happens) :
self.canvasNode.children
instead of your array? I can't test it atm. so it's only a suggestion. – DevAndArtist