0

シェイクイベントに基づくフェードイン/フェードアウト機能がありますが、機能中に再度シェイクすると機能がスタックされます。関数が終了するまで(5秒)待ってから、もう一度リッスンしたいのですが。そのためのコードは何でしょうか?

これは私が現在持っているものです:

function fadeOut ( event )
    transition.to(yes1, {time=2000, alpha=0})
    transition.to(yes2, {time=2000, alpha=0})
    ...
    transition.to(funny3, {time=2000, alpha=0})
    transition.to(funny4, {time=2000, alpha=0})
    timer.performWithDelay(2000, onShakeComplete)
end

responses = {yes1, yes2, yes3, yes4, yes5, yes6, yes7, yes8, yes9, yes10,
no1, no2, no3, no4, no5, maybe1, maybe2, maybe3, maybe4, maybe5,
funny1, funny2, funny3, funny4}

local shaking = false

-- reset shaking flag after a shake is completed
local function onShakeComplete()
    shaking = false
end

local function onShake (event)
        if event.isShake and not shaking then
        shaking = true
        local object = responses[math.random(1,20)]
        transition.to(object, {time=2000, alpha=1})
        timer.performWithDelay(4000, fadeOut)
    end
end
Runtime:addEventListener("accelerometer", onShake)
4

1 に答える 1

3
-- true if we have a shake happening right now
local shaking = false

-- reset shaking flag after a shake is completed
local function onShakeComplete()
    shaking = false
end

local function onShake (event)
    if event.isShake and not shaking then
        local object = responses[math.random(1,20)]
        transition.to(object, {time=2000, alpha=1})
        timer.performWithDelay(4000, fadeOut)
        timer.performWithDelay(4000, onShakeComplete)
        shaking = true
    end
end
于 2012-09-24T17:34:30.273 に答える