GGSound random play

Hi guys,

I just started using GGSound library, and it seems great to manage sound on composer scene transitions.

https://github.com/GlitchGames/GGSound

I have just one question.

local sound = GGSound:new{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ,14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32  } sound:add(audio.loadSound("sounds/jump1.mp3"), "jump1")  sound:add(audio.loadSound("sounds/jump2.mp3"), "jump2")  sound:add(audio.loadSound("sounds/jump3.mp3"), "jump3")

How can I group jump1, jump2 and jump3 sounds that are created in GGSound library and randomly play those three sounds ( via sound:play( “???” ) ) ?

That is easy when I am not using GGSound.

I do it like this:

------------------------------------------------- DECLARATION local jump1 = audio.loadSound ( "sounds/jump1.mp3"  ) local jump2 = audio.loadSound ( "sounds/jump2.mp3"  ) local jump3 = audio.loadSound ( "sounds/jump3.mp3"  ) local Jumps = {jump1, jump2, jump3 } ------------------------------------------------ PLAY local number = math.random(#Jumps) audio.play(Jumps[number])

Waiting your reply!

Many thanks :smiley:

Ivan

Hi, creator of GGSound here, what you want to do is basically the same as what you’re doing when not using GGSound, something like this:

[lua]local number = math.random( 3 )
sound:play( “jump” … number )[/lua]

Hi GG,

Thanks a lot!

I will put your website in Credits section!  :smiley:

I have one last question/concern regarding GGSound usage.

If I have character on screen singing like this:

sound:add( "singing.wav", "singing" ) sound:add( "ouch.wav", "ouch" ) if(character.x \> screenLeft) then sound:play( "singing") end

I want when user touches the character that it stops singing and says “ouch!”

So I assume I do it like this:

local function ouch (event) if(event.phase == "began") then sound:remove( "singing" ) sound:play( "ouch" ) end

My last question is:

It is easy to do this when you have only one song (“singing”)…

But what when you have 3 songs and you choose them using math.random (3), how to stop character signing?

If I remove all 3 songs sounds at the same time then other characters in the game will also stop singing (for example if you have two characters on screen at the same time)?

And I only what character that is touched to stop singing (not the others).

Waiting your reply!

Many thanks!  :slight_smile:

Ivan

Stopping an individual sound is tricky.

If this code is in an enterFrame handler it will try to play the sound 30/60 times a second, which probably isn’t want you want.

[lua]if(character.x > screenLeft) then
sound:play( “singing”)
end[/lua]

What you could do is set a flag around the playing of the singing sound to say if the user is touching or not, and then if they are touching don’t play the singing sound. Then in a touch event handler you can set that flag to true/false depending on whether the phase is began/ended and at the same time you can play the “ouch” sound.

Hi GG,

Thank you.

But that is not what I am looking for.

Let`s say you play “wingFlapping.wav” while the bird(s) are on screen.

When one bird leaves the screen you want to stop that sound for it, but if you use:

sound:remove( "wingFlapping" )

then all birds that are on screen will go to “mute”.

I will try to find a walkaround using:

self.channel = sound:play( "wingFlapping.wav" ) sound:remove( "self.channel" )

Thank you!  :slight_smile:

Ivan

sound:remove() is to remove a loaded sound from the system, not to stop a sound. This is used when unloading sounds i.e. on scene close etc to conserve memory.

They all go mute because the “wingFlapping” sound is now nilled out.

calling sound:remove( “self.channel” ) will only remove a sound that you have called “self.channel” when you loaded it up, which I’m assuming is not what you intended that line to do.

What you are trying to do will not be done with the sound:remove() function.

What you’ll want to do is simply not call sound:play( “wingFlapping” ) on any birds that are not on the screen, and only play that sound when they are.

Thank you GG!  :smiley:

Ivan

All working?

I have 120 - 150 characters, so it will take a couple of days to complete all sound settings :slight_smile:

I will let you know  :slight_smile:

Your library`s I used so far GGData nad GGSound are simply awesome!!!  :smiley:

Glad the libraries are helpful!

Hi GG,

Sounds were set up and everything works great!

Thank you :slight_smile:

Ivan

That’s great!

Hi, creator of GGSound here, what you want to do is basically the same as what you’re doing when not using GGSound, something like this:

[lua]local number = math.random( 3 )
sound:play( “jump” … number )[/lua]

Hi GG,

Thanks a lot!

I will put your website in Credits section!  :smiley:

I have one last question/concern regarding GGSound usage.

If I have character on screen singing like this:

sound:add( "singing.wav", "singing" ) sound:add( "ouch.wav", "ouch" ) if(character.x \> screenLeft) then sound:play( "singing") end

I want when user touches the character that it stops singing and says “ouch!”

So I assume I do it like this:

local function ouch (event) if(event.phase == "began") then sound:remove( "singing" ) sound:play( "ouch" ) end

My last question is:

It is easy to do this when you have only one song (“singing”)…

But what when you have 3 songs and you choose them using math.random (3), how to stop character signing?

If I remove all 3 songs sounds at the same time then other characters in the game will also stop singing (for example if you have two characters on screen at the same time)?

And I only what character that is touched to stop singing (not the others).

Waiting your reply!

Many thanks!  :slight_smile:

Ivan

Stopping an individual sound is tricky.

If this code is in an enterFrame handler it will try to play the sound 30/60 times a second, which probably isn’t want you want.

[lua]if(character.x > screenLeft) then
sound:play( “singing”)
end[/lua]

What you could do is set a flag around the playing of the singing sound to say if the user is touching or not, and then if they are touching don’t play the singing sound. Then in a touch event handler you can set that flag to true/false depending on whether the phase is began/ended and at the same time you can play the “ouch” sound.

Hi GG,

Thank you.

But that is not what I am looking for.

Let`s say you play “wingFlapping.wav” while the bird(s) are on screen.

When one bird leaves the screen you want to stop that sound for it, but if you use:

sound:remove( "wingFlapping" )

then all birds that are on screen will go to “mute”.

I will try to find a walkaround using:

self.channel = sound:play( "wingFlapping.wav" ) sound:remove( "self.channel" )

Thank you!  :slight_smile:

Ivan

sound:remove() is to remove a loaded sound from the system, not to stop a sound. This is used when unloading sounds i.e. on scene close etc to conserve memory.

They all go mute because the “wingFlapping” sound is now nilled out.

calling sound:remove( “self.channel” ) will only remove a sound that you have called “self.channel” when you loaded it up, which I’m assuming is not what you intended that line to do.

What you are trying to do will not be done with the sound:remove() function.

What you’ll want to do is simply not call sound:play( “wingFlapping” ) on any birds that are not on the screen, and only play that sound when they are.

Thank you GG!  :smiley:

Ivan

All working?

I have 120 - 150 characters, so it will take a couple of days to complete all sound settings :slight_smile:

I will let you know  :slight_smile:

Your library`s I used so far GGData nad GGSound are simply awesome!!!  :smiley: