Why is my sound making my game lag in Swift Spritekit?

后端 未结 2 582
日久生厌
日久生厌 2021-01-20 03:58

I have this sound effect when my hero node collects a coin and there is this small hiccup in my game. Its not smooth like in other games when there is sound involved when co

相关标签:
2条回答
  • 2021-01-20 04:05

    you should not be calling prepareToPlay on the collision, prepareToPlay sets up the sound buffer, do that in your didMoveToView function

    0 讨论(0)
  • 2021-01-20 04:14

    Use SKAction.playSoundFileNamed. When you create an SKAction instance ahead of time, it does all the necessary prep to perform the action (in this case, playing a sound) without lag during gameplay. To run the action (play the sound), call runAction on a node — this can be any node, even the scene itself.

    Since it doesn't matter which node you use for sound purposes, use whichever is most convenient. For example, if you're just playing a sound you might call runAction on the scene. But if your sound is part of an action group or sequence that, say, animates a sprite, you could make the sound action part of that sequence and play it on the sprite that you're animating.

    See SpriteKit Programming Guide for more about actions.


    Unrelated Swift tip: use let instead of var for references that won't change. It can help keep you from introducing bugs later, and it probably helps the compiler optimize your code, too.

    0 讨论(0)
提交回复
热议问题