2013-01-14 83 views
0

確定有很多這個問題的答案,但我似乎無法得到它的工作。我的按鈕是在功能場景中創建的:createScene(event),當我進入下一個場景時,我正在嘗試scenePurge並單獨刪除按鈕,但它似乎不工作。在下一個場景中,按鈕不可見,但如果按下按鈕在前一場景中的位置,它仍然處於活動狀態。什麼是刪除這些對象的最佳方法?電暈故事板按鈕沒有從下一個場景中正確移除

 local storyboard = require("storyboard") 
     local scene = storyboard.newScene() 

     --storyboard.removeScene("login") 

--------------------------------------------------------------------------------- 
-- BEGINNING OF YOUR IMPLEMENTATION 
--------------------------------------------------------------------------------- 

local image, image2 


-- Called when the scene's view does not exist: 
function scene:createScene(event) 
    local widget = require("widget") 

    local p1ButtonRelease = function(event) 
    storyboard.gotoScene("maths_dif", "slideLeft", 1000 ) 
     print("touch") 
     return true 
    end 

    local p2ButtonRelease = function(event) 
    storyboard.gotoScene("words", "slideLeft", 1000 ) 
     print("touch") 
     return true 


end 

    local screenGroup = self.view 

    image = display.newImage("images/Background(640x480).png", -20 ,0) 
    image2 = display.newImage("Logo_small.png", 170,0) 
    --screenGroup:insert(image) 

    print("\n2: createScene event") 


    local p1Button = widget.newButton{ 
    default = "images/SinglePlayer_button.png", 
    over = "images/SinglePlayer_button.png", 
    --onPress = button1Press, 
    onRelease = p1ButtonRelease, 
    -- fontSize = 20, 
    -- labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} }, 
    -- label = "+", 
    -- emboss = true 
    } 


    local p2Button = widget.newButton{ 
    default = "images/2Player_button.png", 
    over = "images/2Player_button.png", 
    --onPress = button1Press, 
    onRelease = p2ButtonRelease, 
    -- fontSize = 20, 
    -- labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} }, 
    -- label = "+", 
    -- emboss = true 
    } 

    p1Button.x = 240; p1Button.y = 180 
    p2Button.x = 240; p2Button.y = 260 
end 


-- Called immediately after scene has moved onscreen: 
function scene:enterScene(event) 

    print("2: enterScene event") 

    -- remove previous scene's view 
    storyboard.purgeScene("login") 
    display.remove(p1Button) 
    -- -- Update Lua memory text display 
    -- local showMem = function() 
     image:addEventListener("touch", image) 
     -- text3.isVisible = true 
     -- text2.text = text2.text .. collectgarbage("count")/1000 .. "MB" 
     -- text2.x = display.contentWidth * 0.5 
    -- end 
    -- memTimer = timer.performWithDelay(1000, showMem, 1) 
end 


-- Called when scene is about to move offscreen: 
function scene:exitScene() 

    print("2: exitScene event") 
    storyboard.removeScene("login") 
    storyboard.removeScene("words") 
    storyboard.removeScene("guess_word") 
    storyboard.removeScene("maths_dif") 
    storyboard.removeScene("scene1") 
    display.remove(p1Button) 
    p1Button = nil 
    display.remove(p2Button) 
    p2Button = nil 
    -- remove touch listener for image 
    image:removeEventListener("touch", image) 

end 


-- Called prior to the removal of scene's "view" (display group) 
function scene:destroyScene(event) 

    print("((destroying scene 2's view))") 
end 

--------------------------------------------------------------------------------- 
-- END OF YOUR IMPLEMENTATION 
--------------------------------------------------------------------------------- 

-- "createScene" event is dispatched if scene's view does not exist 
scene:addEventListener("createScene", scene) 

-- "enterScene" event is dispatched whenever scene transition has finished 
scene:addEventListener("enterScene", scene) 

-- "exitScene" event is dispatched before next scene's transition begins 
scene:addEventListener("exitScene", scene) 

-- "destroyScene" event is dispatched before view is unloaded, which can be 
-- automatically unloaded in low memory situations, or explicitly via a call to 
-- storyboard.purgeScene() or storyboard.removeScene(). 
scene:addEventListener("destroyScene", scene) 

--------------------------------------------------------------------------------- 

return scene 

我試圖刪除對象,這樣就不會影響到下一個場景,但不能似乎做到這一點。

+0

你能鏈接你的來源嗎? – Arnold

+0

@Arnold,你在問源代碼嗎?我會補充一些問題 – Gooner

回答

0

已經有人建議在每個場景的開始處使用storyboard.removeAll(),但我個人發現的解決方案不是使用小部件按鈕釋放功能來改變場景,而是創建一個圖像對象並給予它是一個EventListener例如

p1Button = display.newImage("images/SinglePlayer_button.png", 90, 140) 
    p1Button.touch = onSceneTouch 
p1Button:addEventListener("touch", p1Button) 

    local function onSceneTouch(self, event) 
    if event.phase == "began" then 
     storyboard.gotoScene("words", "slideLeft", 1000 ) 
     return true 
    end 
end 

然後,我刪除exitScene()函數中的對象事件偵聽器。在我的下一個場景中,這些先前的按鈕不起作用。

0

您需要將按鈕插入到場景的視圖(組)中。在您的createScene()函數,創建後的按鈕做:

group:insert(p1Button) 

那麼你不必擔心刪除它們,故事情節會爲你做它。

相關問題