1

Im using this code to add background music to my game levels

[self runAction:[SKAction playSoundFileNamed:@"dasdas.mp3" waitForCompletion:NO]];

I want it to stop when Game Over happens. Is there a way to cancel/stop the SKAction, lower volume, anything to stop it?

I know Im suppose to use this code

Instead of:

[node runAction:action withKey:@"BackgroundMusicAction"]

Then stop it by calling the SKNode's method:

- (void)removeActionForKey:(NSString *)key;

with a key you used for creating the action.

but how do I create a key for the first code, playsoundfilenamed and what do I type on the NSString or how do I connect them, Im a NOOB, help please

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • you can't stop a sound started from an SKAction - answer here http://stackoverflow.com/questions/22562493/how-to-stop-a-audio-skaction – bshirley Mar 31 '15 at 20:46

2 Answers2

1

You have the answer in the question itself.

First, you need to assign a key

[self runAction:[SKAction playSoundFileNamed:@"dasdas.mp3"
                           waitForCompletion:NO] 
        withKey: @"BackgroundMusicAction"];

Now, you are running an action with the key BackgroundMusicAction

In order to stop this action, you will call

[self removeActionForKey:@"BackgroundMusicAction"];
BryanH
  • 5,826
  • 3
  • 34
  • 47
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
0

You can choose whatever you put as the key, as long as you use the same for run and remove:

SKAction *action = [SKAction playSoundFileNamed:@"dasdas.mp3"
                              waitForCompletion:NO];
[node runAction:action
        withKey:@"MyGameBackgroundMusicAction"];

Later :

[node removeActionForKey:@"MyGameBackgroundMusicAction"];
BryanH
  • 5,826
  • 3
  • 34
  • 47
rdurand
  • 7,342
  • 3
  • 39
  • 72
  • I get this message "use of undeclared identifier node" – user3653566 May 28 '14 at 18:52
  • In the code above, you call methods on a *node* object. The code you put in your question suggests you might want to use *self* instead here. Note that you must call *runAction* and *removeAction* on the same object. – rdurand May 30 '14 at 07:47