Lua Corona SDK 冲突事件监听器列表

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

我这周刚开始学习Lua,并使用Corona SDK编写2D侧向移动游戏。在这个游戏中,玩家的角色通过按下显示在屏幕上的按钮移动,就像虚拟游戏扳手一样。这些按钮完美地工作,但问题在于我有一个

Runtime:addEventListener("tap", onScreenTap)

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

有没有办法停止在我完成触摸运动键之一时调用shoot函数?我已经尝试过

display.getCurrentStage:setFocus()

并在运动函数的结尾处放置

return true

但似乎什么都没用。

点赞
用户2186639
用户2186639

你可以在每个触摸事件中使用这些基础知识,也可以将所有触摸事件组合到单个函数中以解决问题。

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
        -- 在这里进行操作 --
        display.getCurrentStage():setFocus(event.target)
        event.target.isFocus=true
    elseif event.target.isFocus == true then
        if event.phase == "moved" then
            if inBounds( event ) then
                -- 在这里进行操作 --
            else
                -- 在这里进行操作 --
                display.getCurrentStage():setFocus(nil)
                event.target.isFocus=false
            end
        elseif event.phase == "ended" then
            -- 在这里进行操作 --
            display.getCurrentStage():setFocus(nil)
            event.target.isFocus=false
        end
    end
    return true
end

顺便说一句,如果你尝试在运行时使用这个函数,它会抛出错误。你可以为背景添加事件监听器,或者添加一些控制机制,比如

if event.target then
-- 做一些事情
end
2013-07-24 00:19:45