Хорошо, так что я нашел ответ. Я сделал пару глупых ошибок новичка. Причина, по которой локальная переменная не работала, не имела никакого отношения к моему коду. Это было на самом деле потому, что я неправильно назвал пару файлов изображений (дох!).
Во-вторых, кнопка слушателя не работала, потому что (барабанная дробь ...) я забыл загрузить аудиофайлы заранее (двойная дох!). В итоге я решил разместить функцию playThis вне цикла, чтобы она не создавалась без необходимости. Вместо этого я установил свойство «sound» для созданных кнопок и использовал аргумент события, чтобы выяснить, какая кнопка была нажата, и воспроизвести соответствующий звук.
Вот окончательный код (я сделал его модульным, чтобы сделать его более пригодным для повторного использования). Надеемся, что это будет урок для других в проверке всей вашей среды, прежде чем биться головой о вашем коде.
--init globals
_H = display.contentHeight;
_W = display.contentWidth;
--test to see if I can more efficiently write code to do a repeated action by placing items inside an array and looping through the array
--import the ui file to create buttons
local ui = require("ui")
--create the array. Note that these names will be used for all the objects that refer to these things, so you must have a consistent naming convention
local instruments = {"snare","piano1", "piano2","guitar1","guitar2"}
--set constants to be able to set the x value of the buttons
local function doThis (event)
audio.play(event.target.property)
end
--loop through each item in the array to: (a) load the sound, (b) create a btn press event, and (c) create the button (the event must be created before the listener, else it has nothing to listen for)
local function makeBtns(btnList,btnImg,property,groupXPos,groupYPos)
--first, let's place all the buttons inside a button group, so we can move them together
local thisBtnGroup = display.newGroup();
for index,value in ipairs(btnList) do
--first, we use the value to make some reference strings
local img = "images/base_btn.png"
property = audio.loadSound("sounds/"..value..".wav")
--now create the button
local thisBtn = ui.newButton{
default = img,
onPress = doThis,
text = value,
size = 10
}
thisBtnGroup:insert(thisBtn)
thisBtn.x = (index -1) * thisBtn.width
thisBtn.property = property
end
thisBtnGroup.x = groupXPos; thisBtnGroup.y = groupYPos
return thisBtnGroup
end
local myBand = makeBtns(instruments,"images/base_btn.png","sound",_W/3,_H-50)