试图在 coronasdk 上创建一个退出库存按钮
Trying to make an exit inventory button on coronasdk
我目前正在开发一款需要库存系统的游戏。我不知道如何为我创建的显示对象制作退出按钮。
这是我的代码:
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" 并且返回到点击清单之前所在的屏幕!
在显示组中这样做会更容易,因此您可以使用行命令删除组中的所有对象。
将按钮和函数添加到您的代码中,如下所示:
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,
}
图片是我把你的代码改成这样后剩下的:
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
我目前正在开发一款需要库存系统的游戏。我不知道如何为我创建的显示对象制作退出按钮。
这是我的代码:
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" 并且返回到点击清单之前所在的屏幕!
在显示组中这样做会更容易,因此您可以使用行命令删除组中的所有对象。
将按钮和函数添加到您的代码中,如下所示:
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,
}
图片是我把你的代码改成这样后剩下的:
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