如何将窗口小部件的可见性设置为false?

时间:2018-03-06 03:43:25

标签: lua corona

所以,我有这个功能:

local function addMainMenu()
    local widget = require( "widget" )

    -- Function to handle button events
    local function handleButtonEvent( event )

        if ( "ended" == event.phase ) then
            scene = "GAME"
        end
    end

    -- Create the widget
    local button1 = widget.newButton(
        {
            label = "button",
            onEvent = handleButtonEvent,
            emboss = false,
            -- Properties for a rounded rectangle button
            shape = "roundedRect",
            width = 200,
            height = 40,
            cornerRadius = 2,
            fillColor = { default={0.9,0.9,0.9,1}, over={1,0.1,0.7,0.4} },
            strokeColor = { default={0,0,0,1}, over={0.8,0.8,1,1} },
            strokeWidth = 5
        }
    )

    -- Center the button
    button1.x = display.contentCenterX
    button1.y = display.contentCenterY

    -- Change the button's label text
    button1:setLabel( "Start Game" )
end

添加按钮开始游戏,然后我有:

local function enterFrame()
    local dt = getDeltaTime()
    if (scene == "MAIN_MENU") then
         addMainMenu()
    elseif (scene == "GAME") then
        if (running == false) then
            startGame()
        else 
            moveBg(dt)
            moveEnemy(enemy)
            updateScore()
        end
    elseif (scene == "GAME_OVER") then
        local gameOverLabel = display.newText( "Game Over!", 50, 20, native.systemFont, 16)
        gameOverLabel:setFillColor(1, 1, 1)
    end
end

如您所见,一旦我点击按钮开始,场景就会变为“GAME”,按钮应该消失。问题是:它停留在那里。 And reading the docs,我找不到将其可见性设置为false的方法。如何停止显示小部件?

2 个答案:

答案 0 :(得分:2)

要使其不可见,请使用.isVisible:

button1.isVisible = false

要隐藏它,请使用.alpha

button1.alpha = 0.00
-- or hide just a little by 50%
button1.alpha = 0.50

删除它:

display.remove( button1)
button1= nil

答案 1 :(得分:1)

ButtoWidget继承自GroupObject,继承自DisplayObject,提供属性isVisible

  

<强>概述

     

控制对象是否在屏幕上可见。该   财产也是可读的。默认值为true。

     

示例

     

local rect1 = display.newRect( 100, 100, 50, 50 )

     

rect1:setFillColor( 0.7 )

     

local rect2 = display.newRect( 150, 100, 50, 50 )

     

rect2:setFillColor( 1, 0, 0, 0.6 )

     

rect2.isVisible = false

button1.isVisible = false会隐藏按钮。它将在下一次屏幕更新时消失。

如果您不再需要该按钮,您也可以通过调用button1:removeSelf()或将其从其父组删除来删除它