2013-11-22 62 views
3

我使用Corona SDK和Director 1.4創建應用程序。我的目標是點擊按鈕(btn_play)時打開一個彈出窗口。在Corona SDK中停止事件傳播

但是,我遇到了一個問題。點擊btn_play時,會觸發openPopup(e)以及changeScene(e)(因爲背景設置爲執行該功能)。點擊btn_play按鈕時,如何停止執行功能changeScene(e)

這裏是我的遊戲畫面的代碼:

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 

回答

5

只要把return在你的函數結束。它將阻止touch到底層對象。

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

保持編碼.............. :)

+2

很不錯。它工作正常!完全忽略了'返回真實的重要性' – Raptor

+0

偉大的解決方案!非常感謝。只是一個問題,爲什麼分號? – Rayjax

+0

@Rayjax:分號不是強制性的。它只是用來顯示行結束。 –