试图在coronasdk上制作退出库存按钮

时间:2016-03-16 20:37:10

标签: lua corona inventory

我目前正在开发一款需要库存系统的游戏。我无法弄清楚如何为我创建的显示对象制作一个退出按钮。

这是我的代码:

local Choose_slot = display.newImage("Images/choose_slot.png")
 Choose_slot.x = centerX + 96
 Choose_slot.y = 45

function choose_slot:tap ( event )
  Inventory_Screen = display.newRect( centerX, centerY, 1500, 1500 )
  Inventory_Screen:setFillColor( 0.3, 0.3, 0.3 )
end

local Exit_Button = display.newImageRect( "Images/Exit_Image.png", 32, 32)
Exit_Button.x = centerX + 255
Exit_Button.y = centerY - 135

function Exit_Inventory:tap ( event )
    Inventory_Screen:remove()
    Exit_Button:remove()
end

Exit_Inventory:addEventListener( "tap", Exit_Button)

Choose_slot:addEventListener( "tap", choose_slot)

仅供参考," Choose_slot"是一个提出库存的图像。我想在" Inventory_Screen"出现了" Exit_Inventory"弹出按钮,当您点击它时,它会删除" Inventory_Screen"和" Exit_Button"从屏幕上回到你点击库存之前你所在的屏幕!

2 个答案:

答案 0 :(得分:1)

在显示组中更容易实现这一点,因此您可以使用line命令删除组中的所有对象。

将按钮和功能添加到您的代码中,如下所示:

local function exitFunc(event)
    Choose_slot:removeSelf() -- add whatever you want to remove
    exitB:removeSelf() -- can use object.isVisible = false if you wanted but they maybe touchable still
end 

exitB= widget.newButton
{
  width=135,
  height=60,
  defaultFile = "whatever.png",
  overFile = "whateverOver.png",
  label = "Exit",
  onPress = exitFunc,
}

答案 1 :(得分:1)

enter image description here

图片是我将代码更改为此后剩下的内容:

function Choose_slot:tap ( event )  

        Inventory_Screen = display.newRect( centerX, centerY, 1500, 1500 )
        Inventory_Screen:setFillColor( 0.3, 0.3, 0.3 )

        local function handleExitBEvent( event )   -- ERROR IS BELOW! 

    if ( "ended" == event.phase ) then
        print( "Button was pressed and released" )
        print( "Removing Inventory!" )
        Choose_slot:removeSelf() -- This is not removing -- 
        ExitB:removeSelf()
        Inventory_Screen:removeSelf()
        Inventory_Slot1:removeSelf()
    end
end