Spritekit Adds Sound Effect

I try to add a sound effect to the game whenever the screen is touched. I already have a touchhesBegan method that moves a character, can I put:

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

into this method or I need to make a new method. Also, where should I store the sound file in my project? Is there any place for him, or could it be anywhere?

+4
source share
3 answers

Try the following:

first make sure you put self.userInteractionEnabled = YES;

affects the delegate method when clicked on the 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 must put it in your document resource resource directory application.
+8

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

touchesBegan.

.

+6

Your code is working. And for the file, you can put it anywhere in your project.
You can even create a new folder like "sfx" or something else, and after that you do not need to worry about this path (you do not need to put the folder in the path if you put your "sfx.wav" in the sfx folder, you don’t you need to load it like "sfx / sfx.wav") hehe, what I did last time. Just a head. By the way, you need to enable this .wav good luck

+1
source

Source: https://habr.com/ru/post/1529104/


All Articles