问题
I am using SpriteKit to write a bullet-hell style shoot-em-up, and the SK framework seems to be able to handle hundreds of nodes at 60fps without any problems at all. My problem however is that sometimes I need to spawn 50+ nodes in a single frame, and that does seem to cause the odd hitch in framerate. Are there any tricks I should be using to make sure that creation of many nodes is as performant as possible?
I am re-using SKTextures, should I also have a persistent collection of SKSpriteNodes and SKActions that get 'recycled' instead of creating them new?
回答1:
You can pre-create all nodes before game scene loads(using completion handler) and when needed, just show them i.e. setHidden = NO. This way you don't have to recreate nodes again and again. When nodes are not needed, just set them hidden. You can read more here. Just find part about lasers. I think that would be one way to resolve framerate drop caused by spawning many nodes at same time. And I hope you use atlases...To be sure that everything works correctly enable your draws and nodes count info in view controller and check stats. If you don't use atlases, or use them incorrectly it may happen that draws count is high (comparing to nodes count).
About atlases from the docs:
When you create a texture atlas, you want to strike a balance between collecting too many textures or too few. If you use too few images, Sprite Kit may still need many drawing passes to render a frame. If you include too many images, then large amounts of texture data may need to be loaded into memory at once. Because Xcode builds the atlases for you, you can switch between different atlas configurations with relative ease. So experiment with different configurations of your texture atlases and choose the combination that gives you the best performance.
回答2:
A couple points to keep in mind:
- Remove any nodes no longer needed from parent.
- Do not use usesPreciseCollisionDetection unless you absolutely have to.
- Use a texture atlas.
- Do not go overboard with the number of nodes. Stay realistic and only use the minimum needed.
来源:https://stackoverflow.com/questions/29119936/spritekit-efficiently-create-many-spritenodes