Search code examples
sprite-kitskspritenode

creating new SKSpriteNode vs using a copy of a SKSpriteNode


I'm looking for ways to improve the performance of my game. In my game, the boss will cast 16 fireballs at once (at different angles). My current code is to create a SKSpriteNode for each fireball, and repeat the process 16 times. I can improve the performance by dispatching the creation process to a different thread and then add the sprite back to main thread once it's created. This way I don't see a noticeable lag in the gameplay. However, I'm thinking that will it improve the performance even more if I cache the first fireball sprite after it's being created and then use .copy() 15 times to create the rest of the fireballs? ultimately, I'm trying understand is using .copy() on a SKSpriteNode as expensive as creating new one?

Thanks for any insight.


Solution

  • Copy is a little less expensive, but not much. It doesn't need to access any IO to grab things like the texture since you already have it in memory.

    Now the way you are designing your game, you should not be constantly recreating fireballs. You should create a pool of them, and constantly recycle them. This way you only have to create them once during the loading portion of your game, and you are not wasting precious cycles on redundant work.