Stop event propagation in Corona SDK

五迷三道 提交于 2019-12-12 13:47:37

问题


I'm using Corona SDK, and Director 1.4 to create an app. My goal is to open a popup when the button (btn_play) is clicked.

However, I encounter a problem. When the btn_play is clicked, it triggers openPopup(e) as well as changeScene(e) (as the background is set to execute the function). How can I stop the function changeScene(e) from executing when clicking the btn_play button?

Here is my game screen's codes:

module(..., package.seeall)

local localGroup

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

    -- Background Image
    local background = display.newImageRect("background.jpg", display.contentWidth, display.contentHeight )
    background:setReferencePoint( display.TopLeftReferencePoint )
    background.x, background.y = 0, 0
    background.scene = "scene_menu";

    -- Play button
    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

回答1:


Just put return at the end of your function. It will prevent the touch to underlying objects.

function openPopup(e)
    if(e.phase == "ended") then
        director:openPopUp(e.target.scene);
        return true; -- put this in your function.
    end
end

Keep Coding.............. :)



来源:https://stackoverflow.com/questions/20140217/stop-event-propagation-in-corona-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!