点击按钮时测量体积

下面是一个在运行时测量音量的代码。我想要一个按钮,点击一次后开始测量并执行此代码,再次点击后停止测量。

local g = display.newGroup()

local function newBar()
    local bar = display.newLine(0, 0, 1, 0)
    bar:setColor(0, 55, 100, 20)
    bar.width = 7
    bar.y = 400
    bar.x = 20
    return bar
end

local volumeBar = newBar()

volumeBar.y = 309
volumeBar.x = 320

function volumeBar:enterFrame(event)
    local v = 20 * math.log(r:getTunerVolume())
    local MINTHRESH = 20
    local LEFTMARGIN = 20
    local ONE = -1
    local v2 = MINTHRESH + math.max(v, -MINTHRESH)
    v2 = (display.contentWidth - 20) * v2 / MINTHRESH
    local neg = math.max(20, v2)
    volumeBar.xScale = neg * ONE
    if (v >= -4) then
        volumeBar:setColor(110, 110, 20, 200)
    elseif (v < -4) then
        volumeBar:setColor(235, 80, 80, 233)
    end
end

Runtime:addEventListener("enterFrame", volumeBar)

g:insert(volumeBar)

请帮忙,因为这里需要点击两次。

原文链接 https://stackoverflow.com/questions/6071185

点赞
stackoverflow用户686008
stackoverflow用户686008

在按钮上添加一个tap事件监听器,并在tap函数中调用**Runtime:removeEventListener("enterFrame", volumeBar)**。

2011-05-21 11:20:03