Spritekit:高效创建多个 SpriteNode

Spritekit: Efficiently create many SpriteNodes

我正在使用 SpriteKit 编写一个子弹地狱风格的射击游戏,SK 框架似乎能够以 60fps 的速度处理数百个节点而没有任何问题。然而,我的问题是有时我需要在一帧中生成 50 多个节点,这似乎确实会导致帧率出现奇怪的故障。我应该使用什么技巧来确保尽可能高效地创建多个节点?

我正在重新使用 SKTextures,我是否也应该有一个持久的 SKSpriteNodes 和 SKActions 集合来获得 'recycled' 而不是创建它们新的?

有几点需要牢记:

  1. 从父节点中删除不再需要的任何节点。
  2. 除非绝对必要,否则不要使用 usesPreciseCollisionDetection。
  3. 使用纹理图集。
  4. 节点数量不要过多。保持现实,只使用所需的最低限度。

您可以在游戏场景加载之前预先创建所有节点(使用完成处理程序)并在需要时显示它们,即 setHidden = NO。这样您就不必一次又一次地重新创建节点。当不需要节点时,只需将它们设置为隐藏即可。您可以阅读更多 here。只需找到有关激光的部分。我认为这是解决由于同时产生许多节点而导致的帧率下降的一种方法。我希望您使用地图集……为了确保一切正常,请在视图控制器中启用您的绘制和节点计数信息并检查统计信息。如果您不使用地图集,或者使用不当,可能会出现绘制次数很高的情况(与节点数相比)。

关于来自 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.