Need Help: How to reset button function after event.phase == "ended"

Hi! I’m a super n00b so it’s likely I’m really messing up something simple here but I’ve been working on this for hours and I can’t get it past removeEventListener so it can call back addEventListener.

What I’d like this button to do, is randomly play one of ten sounds from a table I’ve set up (which it does pretty well) but with the button disabled while the sound is playing. Right now it only plays one random sound then will not play anything else (on account of being stopped at removeEventListener). If I get rid of that then it lets you keep clicking the button into eternity and play new sounds over each other until it becomes an echo-ey mess. I tried putting the event.phase == “ended” stuff into a new separate function but it kept giving me the same results of just stopping at removeEventListener.

I thought that the onComplete function might work but based on looking at other people’s use of that I didn’t know how to fit it in.

Also, since the audio files are different lengths I’d like to avoid using a timer.

To summarize: I’m looking to create a button that plays a random sound from a table when touched, is disabled while the sound plays, and then when the sound is over can be clicked again and play another random sound.

[lua]local soundPlaying = true
local removeTouch = true
local resetTouch = true

local function touchListener( event )
if event.phase == “began” then
print( “Touched!” )
local var rand = math.ceil(10*math.random());
audio.play(sounds[rand]);
removeTouch = false

elseif event.phase == “ended” then
print( “No more touching!” )
buttonMain:removeEventListener(“touch”, touchListener)

if (resetTouch) and not (soundPlaying) then
print( “Touched Again!” )
buttonMain:addEventListener(“touch”, touchListener)
end
end
return true;
end
buttonMain:addEventListener(“touch”, touchListener)[/lua]

(print was just so I could see how far it got in the terminal)

Thanks for your help! The forums and tutorials have been incredibly helpful so far.

PS: I wasn’t able to get the variable “rand” to work until I added “var” which I didn’t think was part of Lua, anybody know why that is? [import]uid: 159809 topic_id: 28003 reply_id: 328003[/import]