简体   繁体   中英

SpriteKit properly handling sounds

I have a SpriteKit game where I have a number of different Sprites Lasers, Planes, etc. A number of these Sprites have a sounds that will play whenever they are visible. Right now I play the sound in the SKSpriteNode itself. This probably isn't what I want because if I have I have 50 of the same sprites, they'll all be playing the same sound and gobbing resources. However, I'm not really sure where would be the best place to play the the sound, because for some of these sprites I would need to check if it should be playing or not. I suppose I could put the sounds in the the GameScene and loop through all sprites during every update to determine what sounds should and should not still be playing, that way there would only be one instance of every sound. I was hoping to keep all the sprite code within the sprite itself, but this would solve the issue with multiple instances of the same sound playing. Is this the correct way to handle sounds?

It is hard to give you a proper code example without seeing your code. You can however get the gist of what you can try with what I have below.

In MyScene add a property BOOL propSound .

Whenever you create a new plane and wonder about adding the prop sound do something like this:

if(propSound == false)
{
    propSound = true;
    // add your code to play prop sound
}

If you add every newly created plane sprite into a NSMutableArray like this [yourArrayName addObject:newPlane]; , you can check the array count every time you remove a plane in order to know if the last plane was removed and the sound needs to be stopped.

if([yourArrayName count] == 0)
{
    propSound = false;
    // add your code to stop prop sound
}

Maybe you are looking for something like this?

NSMutableArray *coin = [[NSMutableArray alloc] initWithCapacity:1];
SKAction *_coinSound = [SKAction playSoundFileNamed:@"Pickup_Coin4.wav"   waitForCompletion:NO];
    [coin addObject:_coinSound];
    SKAction *_playCoinSound = [SKAction sequence:coin];

Then I just check if the "character" in my case touched the coin. If so it will play the sound.

if([_adventurer intersectsNode:coin]){
            [coin runAction:_playCoinSound];
}

Edit:

Saw your comment on your question. I guess you could check if there is at least 1 plane on the screen, if so then you could play a loop of the sound. If there is less than 1 no sound.

Do not play the same sound again if there already are playing. You could use a BOOL to see if the sound is already playing.

BOOL isPlaneSoundOn = false;

if(!isPlaneSoundOn && _planeNode.count > 0){
 //play the sound
 isPlaneSoundOn = true;
} else if(_plane.count == 0) {
 //stop the sound
 isPlaneSoundOn = false;
}

Something like that. Maybe you get the idea :)

I would personally play any community sounds off the scene using the SKAction playSoundWithFilename, and assign this sound a key.

Then I would check if the key exists, and if it does not, then play the sound

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM