【问题标题】:Lua Corona SDK Conflicting Event ListenersLua Corona SDK 冲突事件监听器
【发布时间】:2013-07-23 21:23:02
【问题描述】:

[已解决] 感谢您的关注,但我想通了。我需要在我的一些 if 条件中取消嵌套 return true 语句。

我这周刚开始学习 Lua,并开始使用 Corona SDK 编写一个 2D 横向卷轴游戏。在这个游戏中,玩家的角色通过按下屏幕上显示的按钮来移动,就像虚拟游戏手柄一样。这些按钮工作得很好,但是问题是我有一个

Runtime:addEventListener("tap", onScreenTap)

事件监听器,然后调用 shoot() 函数以在注册点击时从玩家发射弹丸。这导致每次我从其中一个移动按钮上抬起触摸时都会发射弹丸。

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

display.getCurrentStage:setFocus()

还有放

return true 

在运动功能结束时,但似乎没有任何效果。

【问题讨论】:

    标签: lua coronasdk event-listener


    【解决方案1】:

    您可以在您拥有的每个触摸功能中使用此基础知识。或者仅将其用于所有触摸事件。将触摸事件组合在一个函数中可能会解决您的问题。

    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
    

    顺便说一句,如果你尝试在运行时使用它,它会抛出错误。您可以将事件侦听器添加到后台,或者只是放置一些控制机制,例如

    if event.target then
    -- blah blah
    end
    

    【讨论】:

    • 感谢Dogancan 的回复,我可以看到这种语法非常有用。但是,我解决了我遇到的问题。
    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多