在CoronaSDK中,我試圖創建一個特定的代碼,它創建了很多按鈕,並通過首先創建一個表來更加「高效」地播放每個按鈕的聲音字符串,然後通過遍歷表來創建按鈕和處理程序。我正在使用通用的「ui.lua」文件來創建按鈕。但是,我得到(至少)兩個錯誤。第一個是newButton fcn表示它期望「默認值」的表值。下面的代碼:使用ui庫,同時在coronaSDK中迭代表中
--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","softPiano"}
--set constants to be able to set the x value of the buttons
local h = display.contentWidth/6-25;
local multiplier = 1
--loop through each item in the array to: (a) load the sound, (b) create a btn press event, and (c) create the button
for k,v in pairs(instruments) do
--first, we use the value to make some reference strings
img = "images/"..v.."_btn.png"
sound = "media/"..v..".wav"
--now create the event listener
local playThis = function (event)
audio.play(sound)
end
--now create the button
local thisInstrument = ui.newButton{
default = img,
onPress = playThis
}
thisInstrument.x = h*multiplier
thisInstrument.y = display.contentHeight * .8
multiplier = multiplier + 1
end
當我默認的值更改爲一個直線上升的字符串,都至少創建和顯示的按鈕在屏幕上如預期。
local thisInstrument = ui.newButton{
default = "images/snareDrum_btn.png",
onPress = playThis
}
當然,單擊按鈕時聲音仍然不起作用。所以,有兩個問題:首先,爲什麼不會簡單引用default = img工作?其次,我如何讓聽衆爲每個新按鈕工作?