在coronaSDK中迭代表时使用ui库

时间:2011-09-23 01:23:58

标签: corona

在CoronaSDK中,我试图创建一个代码,通过首先创建一个字符串表然后通过迭代创建按钮和处理程序来创建大量按钮并为每个按钮播放更“高效”的声音桌子。我正在使用常见的“ui.lua”文件来创建按钮。但是,我得到(至少)两个错误。第一个是newButton fcn说它期望“默认”的表值。这是代码:

--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工作?其次,如何让听众为每个新按钮工作?

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了答案。我做了几个愚蠢的菜鸟错误。局部变量不起作用的原因与我的代码无关。这实际上是因为我错误地命名了几个图像文件(doh!)。

其次,按钮监听器没有工作,因为(鼓号......)我忘了提前加载音频文件(双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)