Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping/Pausing a sound that is set to repeat forever in SpriteKit Swift 3

I'm having an issue and would really like it if someone could help out, basically I have a sequence of two sounds that are set to repeat forever. However, I want it so that I can trigger the sound to stop.

All I really need to know is:

In Swift 3 SpriteKit, what code do I use to stop, or pause, a repeating sequence of sounds indefinitely?

Edit: I was using SKActions to play the sounds, the issue has now been solved.

like image 868
Ethan Humphries Avatar asked Sep 11 '25 12:09

Ethan Humphries


1 Answers

Always good practice to show some code on stack overflow, with your current question we can only take guesses.

If you are using SKActions for sound you can give the repeat action a key and remove that action later. Create the key like so

 class GameScene: SKScene {

       let soundKey = "RemoveSoundKey" // this avoids typos
 }

and than change your action to this

 let repeatAction = SKAction.repeatActionForever(YOURSOUNDACTION)
 run(repeatAction, withKey: soundKey)

Than you can say

 removeAction(forKey: soundKey)

when you want to stop the repeat action.

Note: If you run the action on a node e.g

 player.run(repeatAction...)

dont forget to call the remove action on the node

 player.removeAction(forKey: soundKey)

Hope this helps

like image 135
crashoverride777 Avatar answered Sep 14 '25 02:09

crashoverride777