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

我正在尝试在Corona上制作一个类似于十五游戏的东西。我创建了一些瓷砖,并为每个瓷砖分配了一个eventListener,它可以检测滑动和滑动的检测,然后执行调用移动瓷砖的函数。我遇到了一个问题,如果在我的瓷砖移动过程中进行滑动,它会改变方向,而且行为不可接受。例如,我需要将其向下移动90px,但当它向下移动45px时,如果我向右滑动,它将向下移动45px和向右移动45px。如何临时禁用eventListener以避免这种行为?我已经关于这个项目提出了问题,代码位于[这里](https://stackoverflow.com/questions/21028931/oop-and-eventlistener-in-lua-corona-sdk),因此我不重新发布它。非常感谢。

点赞
用户869951
用户869951

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

function swipeListener(event)
    if moving then return end -- 在移动期间忽略滑动
    setup motion
    moving = true
end

你需要一种方法来知道运动何时完成,比如一个指示“动画完成”的事件或者一个enterFrame事件处理程序,其中你检查你的盒子是否到达目标位置,如果是,你设置 moving = false:

function swipeListener(event)
    if moving then return end -- 在移动期间忽略滑动
    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

你可能有其他知道运动何时完成的方式,所以以上的一些可能不适用,但你明白了。

2014-01-27 02:22:59