Corona SDK,在对象移动时暂时禁用事件监听器

时间:2014-01-26 23:25:46

标签: lua corona

我正试图在Corona上制作十五个游戏。我创建了一个tile并为每个tile分配了一个eventListener,它检测滑动和滑动的检测,然后执行一个移动tile的函数调用。我遇到了一个问题,如果我在我的瓷砖移动过程中滑动它会改变它的方向并且它的行为是不可接受的。例如,我需要向下移动90px,但是当它向下移动45px时如果我向右滑动它将向下移动45px并向右移动45px。 如何临时禁用eventListener以避免此行为? 我已经问过关于这个项目的问题,代码是here,因此我没有重新发布它。 非常感谢你。

1 个答案:

答案 0 :(得分:3)

最好有一个标志,用于确定当标志为真时你的盒子是否接受事件(以下是伪代码):

function swipeListener(event)
    if moving then return end -- ignore swipe while moving
    setup motion 
    moving = true
end

您需要一种了解动画何时完成的方法,例如指示“动画完成”的事件,或者可能是enterFrame事件处理程序,在该处理程序中检查您的框是否已达到分散,如果是,则设置moving = false:

function swipeListener(event)
    if moving then return end -- ignore swipe while moving
    setup motion 
    Runtime:addEventListener(enterFrame, "enterFrame")
    moving = true
end

function enterFrame(event)
    if moving then 
        check if motion done
        if yes then 
            moving = false
            stop motion 
            Runtime:removeEventListener(enterFrame, "enterFrame")
        end
    end
end

您可能有其他方式知道动作何时完成,因此上述某些内容可能不适用,但您明白了。