【问题标题】:Trying to make an exit inventory button on coronasdk尝试在 coronasdk 上制作退出库存按钮
【发布时间】:2016-03-16 20:37:09
【问题描述】:

我目前正在开发一款需要库存系统的游戏。我不知道如何为我创建的显示对象制作退出按钮。

这是我的代码:

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”并返回到你之前的屏幕你点击了库存!

【问题讨论】:

    标签: lua coronasdk inventory


    【解决方案1】:

    图片是我把你的代码改成这样后留下的:

    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
    

    【讨论】:

    • 谢谢!效果很好!完成后,我会将您添加到游戏积分中!但是有一个问题,是否有任何理由删除库存槽 1 或者这是一个错误?谢谢
    • 祝游戏好运。
    【解决方案2】:

    在显示组中这样做会更容易,因此您可以使用行命令删除组中的所有对象。

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

    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,
    }
    

    【讨论】:

    • 感谢您的回复,我已尝试使用您的代码,但仍然没有运气。我收到一条错误消息:“游戏:lua:72:尝试索引全局'小部件'(一个零值)。我很困惑,因为我在代码中明确定义了一个小部件。你能帮忙吗?
    • 当您的对象之一是本地的但您在全球范围内访问时。只需删除所有本地关键字即可查看问题所在,否则显示整个文件以便我检查。
    • 好的,这是我的代码:pastebin.com/EisBsNqx——我遇到的问题是库存屏幕的小部件部分。我想要它,所以当您点击屏幕上的图像时,它会将您带到库存屏幕,屏幕内部是一个退出按钮(不起作用的小部件),退出按钮只能在按下库存按钮时出现。希望这对你有帮助吗?我还需要让退出按钮退出库存屏幕,移除按钮并将屏幕返回到您在进入库存屏幕之前所在的位置。谢谢
    • 您是否在代码的开头包含了 local widget = require( "widget" ) ?您的错误是第 72 行,将整个项目压缩并通过电子邮件发送给我 amirfarazmand@gmail.com
    • 刚检查,你要加上这个defaultFile = "Images/Exit_Image.png", overFile = "Images/Exit_Image_Over.png",
    猜你喜欢
    • 2021-04-22
    • 2013-09-20
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多