Reputation: 5
I'm an extreme beginner to Corona SDK, and I am currently attempting to make a sound board, where the screen displays multiple buttons, and each button you tap makes a different sound. I am using a process of duplicating an image and having each duplicate play a sound, but I ran into some problems.
Is there a way that I can create "clones" of display objects? What I mean, is that I want to spawn multiple images on the screen, each having some sort of unique value so when one of them is clicked, I will be able to recognize which one.
Upvotes: 0
Views: 496
Reputation: 2833
Try this out:
local function onClickButton( event )
local button = event.target
if event.phase == "ended" then
audio.stop() -- Stop ALL current channels
audio.play( button.stream )
end
end
local function createButton( params )
local x = params.x or 0
local y = params.y or 0
local audio_location = params.audio or "my_sound.mp3"
local button = display.newRect( x, y, 50, 50 )
button.stream = audio.loadStream( audio_location )
button:addEventListener( "touch", onClickButton )
end
createButton( { x = 100, y = 100, audio = "my_sound.mp3" } )
createButton( { x = 200, y = 100, audio = "my_sound_2.mp3" } )
createButton( { x = 100, y = 200, audio = "my_sound_3.mp3" } )
createButton( { x = 200, y = 200, audio = "my_sound_4.mp3" } )
Windows Phone doesn't support MP3's so have that in mind if you're planning on target that market as well: https://docs.coronalabs.com/guide/media/audioSystem/index.html
You can also use modular classes with metatables
but I don't think it's necessary in this case but here is more information about that:
https://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/
Upvotes: 0