user3299383
user3299383

Reputation: 223

Spritekit adding a sound effect

I'm trying to add a sound effect to a game whenever the screen is touched. I already have a touchesBegan method that moves a character, can i put the :

[SKAction playSoundFileNamed:@"sfx.wav" waitForCompletion:NO];

into that method or do I need to make a new method. Also, where should I store the sound file in my project? Is there a certain place for it or can it just be anywhere?

Upvotes: 4

Views: 6244

Answers (3)

Dylan Yasen
Dylan Yasen

Reputation: 21

Your code works. And for the file, you can put it anywhere in your project .
You can even create a new folder like "sfx" or something, and after this you don't need to worry about it's path(you dont have to put the folder in the path, if you put your "sfx.wav" in sfx folder, you dont need to load it like "sfx/sfx.wav")hehe, that's what i did last time. Just a heads up. BTW, you do need to include that .wav good luck

Upvotes: 1

Dhaval Bhadania
Dhaval Bhadania

Reputation: 3089

Try this one :

make sure first you have put self.userInteractionEnabled = YES;

touches delegate method when clicked on screen:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
      // For play your wav file here
      [self runAction:[SKAction playSoundFileNamed:@"sfx.wav" waitForCompletion:NO]];

      // if you want do with touches point do here 
      for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInNode:self];
        //do your stuff here
      }
}

where should I store the sound file in my project?

  • you have to put it in your application of document directory mean resource folder.

Upvotes: 8

Andrey Gordeev
Andrey Gordeev

Reputation: 32539

Just put

[self runAction:[SKAction playSoundFileNamed:@"sfx.wav" waitForCompletion:NO]];

line into touchesBegan method.

You can place audio file anywhere in your project.

Upvotes: 6

Related Questions