Updating variable on click

Hi Folks,

I'm currently updating a variable +1 on tap of an image.

This all works and updates.

My next part is I'm trying to change the sound every time a number is reached.

For example, if the variable reaches 21, it would stop the previous audio and play the next one and so on every time it reaches certain marks.

I managed to do something like that but it slowed down other stuff happening and my thoughts are that it was reloading the sound every time the person tapped and changed the number.

Could someone please assist me and tell me where i'm going wrong.

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
33
34
35
36
37
38
local soundScore = 0
 
local function updateSounds (event)
        soundScore = soundScore + 1
        hits.text = soundScore --To be deleted.
end
button:addEventListener("tap", updateSounds)
 
-------------------------------------------
--                      SOUNDS                                    --
-------------------------------------------
 
 
if soundScore < 10 then
        -- Sound
                s001 = audio.loadStream( "sound1.wav" )
                audio.play( s001, { channel= 7, loops=-1} )
end
if soundScore > 20 then
        audio.stop( 7 )
                -- Sound
                s002 = audio.loadStream( "Sound2.wav" )
                audio.play( s002, {channels=8, loops=-1} )
end
if soundScore > 30 then
        audio.stop( 8 )
        audio.dispose ( 8 )
                -- Sound
                s003 = audio.loadStream( "Sound3.wav" )
                sChannel = audio.play( s003, { channel=9, loops=1}  )
end
if soundScore > 40 then
        audio.stop(9)
        audio.dispose(9)
                -- Sound
                s004 = audio.loadStream( "Sound4.wav" )
                sChannel = audio.play( s004, { channel=10, loops=1}  )
end

How long are your sound clips? Are these just sound effects, or are they long playing music tracks?

Are they optimized for mobile (mono, 11khz vs. stereo 44K -- huge difference in file size and memory usage, which translates to speed when loading)?

Why are you playing them on different channels?

Hi robmiracle,

Thanks for the reply,

I have checked the sounds and they are in that format.

They are 5 second sound clips which loop until told to stop.

I am playing them on different channels as that was what i thought was the best way so maybe my problem starts there.

Does the code i have written look ok to you?

Thanks

Couple of things. I would consider using audio.load() instead of audio.loadStream(), four 5 second clips shouldn't eat up a lot memory and you're using them all the time. The loading of files is a slow down. You want to use audio.loadStream for like 2-3 minute long clips or longer so that those long load times are spread out over the app run time, but in your case its easier to pay the price up front to load the clips.

You need channels to handle when you are playing multiple sounds at once. Like in my game OmniBlaster (http://bit.ly/omniblaster), I have a background music track that plays continuously. I have explosions going on, weapons fire going on. Some of that weapons fire is a brief under 1 sec clip, others are upwards of 5 seconds. Then when the boss is on the screen there is a low rumbling sounds that loops while he's on the screen. Each sound as it plays takes up a channel. Corona is really good about automatically finding a channel for you, so I let all my sfx fine a channel and only specifically deal with the background sounds on a set channel. Using channels also allows you to control the volume specifically on that channel. In your case, I don't really see much of a need for you to worry about the channel.

As for your looping sounds, in two places you list the loops as -1 and in two others you have as just 1. So I'm a bit confused if those guys are supposed to loop too.

As for the code, I might think about doing this:

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
local soundScore = 0
local sounds = {}
local sounds[1] = audio.load( "sound1.wav" )
local sounds[2] = audio.load( "Sound2.wav" )
local sounds[3] = audio.load( "Sound3.wav" )
local sounds[4] = audio.load( "Sound4.wav" )
 
 
local playSound(index)
     audio.stop()
     audio.play(sounds[index), { loops=-1 })
end
 
local function updateSounds (event)
        soundScore = soundScore + 1
        hits.text = soundScore --To be deleted.
        if soundScore < 10 then
              playSound(1)
        elseif soundScore > 20 then
              playSound(2)
        elseif soundScore > 30 then
              playSound(3)
        elseif soundScore > 40 then
              playSound(4)
        end
        return true
end
button:addEventListener("tap", updateSounds)
 
views:1764 update:2012/2/9 11:37:26
corona forums © 2003-2011