在Corona SDK中停止事件传递

我正在使用Corona SDK和Director 1.4创建应用程序。我的目标是在单击按钮(btn_play)时打开一个弹出窗口。

不过,我遇到一个问题。当单击btn_play时,它触发openPopup(e)以及changeScene(e)(因为背景设置为执行该函数)。如何阻止单击btn_play按钮时执行changeScene(e)函数?

这是我的游戏屏幕代码:

module(..., package.seeall)

local localGroup

function new()
    localGroup = display.newGroup();

    -- 背景图像
    local background = display.newImageRect("background.jpg", display.contentWidth, display.contentHeight )
    background:setReferencePoint( display.TopLeftReferencePoint )
    background.x, background.y = 0, 0
    background.scene = "scene_menu";

    -- 播放按钮
    local btn_play = display.newImageRect("grass.png", 320, 82 )
    btn_play:setReferencePoint( display.CenterReferencePoint )
    btn_play.x = display.contentWidth * 0.5
    btn_play.y = 600
    btn_play.scene = "inventory"

    localGroup:insert(background);
    localGroup:insert(btn_play);

    function changeScene(e)
        if(e.phase == "ended") then
            director:changeScene(e.target.scene);
        end
    end

    function openPopup(e)
        if(e.phase == "ended") then
            director:openPopUp(e.target.scene);
        end
    end

    background:addEventListener("touch", changeScene);
    btn_play:addEventListener("touch", openPopup);

    return localGroup;
end
点赞
用户1979583
用户1979583
只需要在函数的结尾处加上 `return`。这样可以防止`touch`事件传递给底层对象。
function openPopup(e)
    if(e.phase == "ended") then
        director:openPopUp(e.target.scene);
        return true; -- 把这个放在你的函数里
    end
end

继续编码…… :)

2013-11-22 14:00:29