Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 249
user3299383 Avatar asked Dec 06 '22 03:12

user3299383


1 Answers

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.
like image 83
Dhaval Bhadania Avatar answered Dec 24 '22 07:12

Dhaval Bhadania