Point taken on the naming, apologies.
I’m definitely a little confused. Because the value of the plate1Sound, plate2Sound, plate3Sound etc. variables changes depending on what chord has been triggered and it pulls that value from the local ‘strum’ table higher up in the code.
How can I load plate1Sound/plate2Sound etc as local variables when the value is not fixed?
This is the entire main.lua file. When a chord button is touched, the value of each of the plates changes and can be triggered by a touch event. Have I just done this completely and utterly wrong in a structural sense? I just don’t understand how I can have plateSound be a local variable when it is pulling the sounds from the strum table of local variables.
Sorry for all the questions. I wonder if I have misunderstood something bigger than I realised.
EDIT: wait, do you mean I can set up an empty local variable for each plateSound at the top of the file, as below? This is getting incredibly unwieldy and I haven’t even set up all 24 plates yet O_O
-- Set width and height \_W = display.contentWidth \_H = display.contentHeight --activate multitouch system.activate = ("multitouch") ---------------------------------------------- -- LOAD IMAGES ---------------------------------------------- local background = display.newImage ("background.png") background:scale( 2, 2 ) ----------------------------------------------- --MAJOR BUTTONS ----------------------------------------------- local buttons = {} buttons.killbutton = display.newImage ("c-button.png") buttons.killbutton:scale ( 0.65, 0.65 ) buttons.killbutton.x = 40 buttons.killbutton.y = 100 buttons.cbutton = display.newImage ("c-button.png") buttons.cbutton:scale ( 0.75, 0.75 ) buttons.cbutton.x = 40 buttons.cbutton.y = 190 buttons.gbutton = display.newImage ("g-button.png") buttons.gbutton:scale ( 0.75, 0.75 ) buttons.gbutton.x = 90 buttons.gbutton.y = 190 ------------------------------ -- STRUMPLATE ------------------------------ local strumplate = {} strumplate.plate1 = display.newImage ("strumplate.png") strumplate.plate1.x = 400 strumplate.plate1.y = 280 strumplate.plate2 = display.newImage ("strumplate.png") strumplate.plate2.x = 400 strumplate.plate2.y = 270 strumplate.plate3 = display.newImage ("strumplate.png") strumplate.plate3.x = 400 strumplate.plate3.y = 260 strumplate.plate4 = display.newImage ("strumplate.png") strumplate.plate4.x = 400 strumplate.plate4.y = 250 strumplate.plate5 = display.newImage ("strumplate.png") strumplate.plate5.x = 400 strumplate.plate5.y = 240 strumplate.plate6 = display.newImage ("strumplate.png") strumplate.plate6.x = 400 strumplate.plate6.y = 230 ---------------------------------------------- -- LOAD MAIN BUTTON SOUNDS -------------------------------------- local ebmaj = audio.loadSound ("ebmaj.wav") local bbmaj = audio.loadSound ("bbmaj.wav") local fmaj = audio.loadSound ("fmaj.wav") local cmaj = audio.loadSound ("cmaj.wav") local gmaj = audio.loadSound ("gmaj.wav") local dmaj = audio.loadSound ("dmaj.wav") local amaj = audio.loadSound ("amaj.wav") local emaj = audio.loadSound ("dmaj.wav") local bmaj = audio.loadSound ("amaj.wav") local ebmin = audio.loadSound ("ebmaj.wav") local bbmin = audio.loadSound ("bbmaj.wav") local fmin = audio.loadSound ("fmaj.wav") local cmin = audio.loadSound ("cmaj.wav") local gmin = audio.loadSound ("gmaj.wav") local dmin = audio.loadSound ("dmaj.wav") local amin = audio.loadSound ("amaj.wav") local emin = audio.loadSound ("dmaj.wav") local bmin = audio.loadSound ("amaj.wav") local eb7 = audio.loadSound ("ebmaj.wav") local bb7 = audio.loadSound ("bbmaj.wav") local f7 = audio.loadSound ("fmaj.wav") local c7 = audio.loadSound ("cmaj.wav") local g7 = audio.loadSound ("gmaj.wav") local d7 = audio.loadSound ("dmaj.wav") local a7 = audio.loadSound ("amaj.wav") local e7 = audio.loadSound ("dmaj.wav") local b7 = audio.loadSound ("amaj.wav") local chords = { ebmaj, bbmaj, fmaj, cmaj, gmaj, dmaj, amaj, emaj, bmaj, ebmin, bbmin, fmin, cmin, gmin, dmin, amin, emin, bmin, eb7, bb7, f7, c7, g7, d7, a7, e7, b7 } -------------------------------------- -- STRUMPLATE SOUNDS LOADED -------------------------------------- local bb1 = audio.loadSound ("strumbb1.wav") local b1 = audio.loadSound ("strumb1.wav") local c1 = audio.loadSound ("strumc1.wav") local cs1 = audio.loadSound ("strumcs1.wav") local d1 = audio.loadSound ("strumd1.wav") local eb1 = audio.loadSound ("strumds1.wav") local e1 = audio.loadSound ("strume1.wav") local f1 = audio.loadSound ("strumf1.wav") local fs1 = audio.loadSound ("strumfs1.wav") local g1 = audio.loadSound ("strumg1.wav") local gs1 = audio.loadSound ("strumgs1.wav") local a1 = audio.loadSound ("struma1.wav") local bb2 = audio.loadSound ("strumbb2.wav") local b2 = audio.loadSound ("strumb2.wav") local c2 = audio.loadSound ("strumc2.wav") local cs2 = audio.loadSound ("strumcs2.wav") local d2 = audio.loadSound ("strumd2.wav") local eb2 = audio.loadSound ("strumds2.wav") local e2 = audio.loadSound ("strume2.wav") local f2 = audio.loadSound ("strumf2.wav") local fs2 = audio.loadSound ("strumfs2.wav") local g2 = audio.loadSound ("strumg2.wav") local gs2 = audio.loadSound ("strumgs2.wav") local a2 = audio.loadSound ("struma1.wav") local bb3 = audio.loadSound ("strumbb3.wav") local b3 = audio.loadSound ("strumb3.wav") local c3 = audio.loadSound ("strumc3.wav") local cs3 = audio.loadSound ("strumcs3.wav") local d3 = audio.loadSound ("strumd3.wav") local eb3 = audio.loadSound ("strumds3.wav") local e3 = audio.loadSound ("strume3.wav") local f3 = audio.loadSound ("strumf3.wav") local fs3 = audio.loadSound ("strumfs3.wav") local g3 = audio.loadSound ("strumg3.wav") local gs3 = audio.loadSound ("strumgs3.wav") local a3 = audio.loadSound ("struma1.wav") local bb4 = audio.loadSound ("strumbb4.wav") local b4 = audio.loadSound ("strumb4.wav") local c4 = audio.loadSound ("strumc4.wav") local cs4 = audio.loadSound ("strumcs4.wav") local d4 = audio.loadSound ("strumd4.wav") local eb4 = audio.loadSound ("strumds4.wav") local e4 = audio.loadSound ("strume4.wav") local f4 = audio.loadSound ("strumf4.wav") local fs4 = audio.loadSound ("strumfs4.wav") local g4 = audio.loadSound ("strumg4.wav") local gs4 = audio.loadSound ("strumgs4.wav") local a4 = audio.loadSound ("struma1.wav") local strum = { bb1, b1, c1, cs1, d1, eb1, e1, f1, fs1, g1, gs1, a1, bb2, b2, c2, cs2, d2, eb2, e2, f2, fs2, g2, gs2, a2, bb3, b3, c3, cs3, d3, eb3, e3, f3, fs3, g3, gs3, a3, bb4, b4, c4, cs4, d4, eb4, e4, f4, fs4, g4, gs4, a4 } local plate1Sound = nil local plate2Sound = nil local plate3Sound = nil local plate4Sound = nil local plate5Sound = nil local plate6Sound = nil --------------------------------------- -- FUNCTIONS --------------------------------------- audio.setVolume( 0.2, { channel=1 } ) function playC (event) audio.stop({channel=1}) audio.play(chords[1], { channel=1, loops=-1 }) plate1Sound = strum[2] plate2Sound = strum[4] plate3Sound = strum[5] plate4Sound = strum[7] plate5Sound = strum[9] plate6Sound = strum[10] local isPlate1Playing = audio.isChannelPlaying( 2 ) if isPlate1Playing then plate1Stop = system.getTimer() plate1Current = plate1Stop - plate1Start print("plate 1 " .. plate1Current) if plate1Current \< 2000 then audio.stop( 2 ) audio.play(plate1, { channel=2 }) audio.seek( plate1Current, {channel=2} ) end end local isPlate2Playing = audio.isChannelPlaying( 3 ) if isPlate2Playing then plate2Stop = system.getTimer() plate2Current = plate2Stop - plate2Start print("plate 2 " .. plate2Current) if plate2Current \< 2000 then audio.stop( 3 ) audio.play(plate2, { channel=3 }) audio.seek( plate2Current, { channel=3 } ) end end end function playG (event) audio.stop({channel=1}) audio.play(chords[2], { channel=1, loops=-1 }) plate1Sound = strum[1] plate2Sound = strum[3] plate3Sound = strum[5] plate4Sound = strum[6] plate5Sound = strum[8] plate6Sound = strum[10] local isPlate1Playing = audio.isChannelPlaying( 2 ) if isPlate1Playing then plate1Stop = system.getTimer() plate1Current = plate1Stop - plate1Start print("plate 1 " .. plate1Current) if plate1Current \< 2000 then audio.stop( 2 ) audio.seek(plate1Current, {channel=2} ) audio.play(plate1, { channel=2 }) end end local isPlate2Playing = audio.isChannelPlaying( 3 ) if isPlate2Playing then plate2Stop = system.getTimer() plate2Current = plate2Stop - plate2Start print("plate 2 " .. plate2Current) if plate2Current \< 2000 then audio.stop( 3 ) audio.seek( plate2Current, {channel=3} ) audio.play(plate2, { channel=3 }) end end end function onPlate1 (event) audio.play(plate1Sound, { channel=2, duration=2000 }) plate1Start = system.getTimer() end function onPlate2 (event) audio.play(plate2Sound, { channel=3, duration=2000 }) plate2Start = system.getTimer() end function onPlate3 (event) audio.play(plate3Sound, { channel=4, duration=2000 }) end function onPlate4 (event) audio.play(plate4Sound, { channel=5, duration=2000 }) end function onPlate5 (event) audio.play(plate5Sound, { channel=6, duration=2000 }) end function onPlate6 (event) audio.play(plate6Sound, { channel=7, duration=2000 }) end local function killall (event) audio.stop() currentTime = 0 end ------------------------------------------ --EVENT LISTENERS ------------------------------------------ buttons.killbutton:addEventListener ("tap", killall) buttons.cbutton:addEventListener('tap', playC) buttons.gbutton:addEventListener('tap', playG) strumplate.plate1:addEventListener('touch', onPlate1) strumplate.plate2:addEventListener('touch', onPlate2) strumplate.plate3:addEventListener('touch', onPlate3) strumplate.plate4:addEventListener('touch', onPlate4) strumplate.plate5:addEventListener('touch', onPlate5) strumplate.plate6:addEventListener('touch', onPlate6)