Lua Corona SDK冲突事件监听器



[SOLVED]谢谢你的查找,但我找到了。我需要在一些if条件中取消嵌套return true语句。

本周我刚刚开始学习Lua,并且我已经开始使用Corona SDK编程一个2D侧滚动游戏。在这个游戏中,玩家的角色通过按下屏幕上显示的按钮来移动,就像虚拟游戏板一样。这些按钮工作得很好,然而,问题是我有一个

Runtime:addEventListener("tap", onScreenTap)

事件侦听器,然后调用shoot()函数,在注册敲击时从播放器发射炮弹。这会导致每次我按下其中一个移动按钮时都会发射一枚炮弹。

当我触摸完其中一个移动键时,有什么方法可以阻止拍摄功能调用吗?我试过

display.getCurrentStage:setFocus()

并且还放置

return true 

在运动结束时起作用,但似乎什么都不起作用。

您可以在拥有的每个触摸功能中使用这些基本功能。。或者只适用于所有触摸事件。将触摸事件组合到一个功能中可能会解决您的问题。

function inBounds( event )
    local bounds = event.target.contentBounds
    if event.x > bounds.xMin and event.x < bounds.xMax and event.y > bounds.yMin and event.y  < bounds.yMax then
        return true
    end
    return false
end 

function touchHandler( event )
    if event.phase == "began" then
        -- Do stuff here --
        display.getCurrentStage():setFocus(event.target)
        event.target.isFocus=true
    elseif event.target.isFocus == true then
        if event.phase == "moved" then
            if inBounds( event ) then
                -- Do stuff here --
            else
                -- Do stuff here --
                display.getCurrentStage():setFocus(nil)
                event.target.isFocus=false
            end
        elseif event.phase == "ended" then
            -- Do stuff here --
            display.getCurrentStage():setFocus(nil)
            event.target.isFocus=false
        end
    end
    return true
end

顺便说一句,如果你试图在Runtime中使用它,它会抛出并出错。您可以在后台添加一个事件侦听器,或者只添加一些控制机制,如

if event.target then
-- blah blah
end

最新更新