2011-09-23 18 views
0

在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工作?其次,我如何讓聽衆爲每個新按鈕工作?

回答

0

好的,所以我找到了答案。我犯了幾個愚蠢的新秀錯誤。局部變量不起作用的原因與我的代碼無關。這實際上是因爲我錯誤地命名了幾個圖像文件(doh!)。

其次,按鈕監聽器沒有工作,因爲(drumroll ...)我忘記了提前加載音頻文件(double doh!)。我最終決定將playThis函數放在循環之外,這樣它就不會被不必要地重新創建。相反,我在創建的按鈕上設置了「聲音」屬性,並使用事件參數來確定按下哪個按鈕並播放相應的聲音。

下面是最終的代碼(我將它模塊化以使其更加可重用)。希望這會成爲其他人在上你的代碼之前檢查你的整個環境的教訓。

--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) 
相關問題