user3653566
user3653566

Reputation: 21

How to stop/cancel PlaySoundFileNamed?

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

Upvotes: 1

Views: 906

Answers (2)

ZeMoon
ZeMoon

Reputation: 20284

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"];

Upvotes: 1

rdurand
rdurand

Reputation: 7410

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"];

Upvotes: 0

Related Questions