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
you should not be calling prepareToPlay on the collision, prepareToPlay sets up the sound buffer, do that in your didMoveToView function
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.