【问题标题】:How to design a "Dynamic inventory system" for a point and click game?如何为点击游戏设计“动态库存系统”?
【发布时间】:2012-09-27 07:33:45
【问题描述】:

我在 Lua 和 Corona 中对点击游戏的库存系统进行了大量研究。 我遇到过这个例子,我正在做类似的事情,但我需要一个动态库存系统。 我的意思是,如果我有 4 个插槽,并且它们都已满,则第五个对象转到下一个插槽,因此右侧会有一个箭头,因此我可以单击 ; 并转到下一页。 假设有 5 个项目,我有 4 个插槽,第五个插槽将在下一页上。 我使用第三个项目,然后第三个插槽将是空的,所以我希望第四个和第五个项目自动移回第三个和第四个插槽。 我很难弄清楚这一点。 感谢您的提前。

local myInventoryBag={}
local maxItems = 10 -- change this to how many you want

myInventoryBag[5]=3 -- Hammer for instance
myInventoryBag[4]=7 -- A metal Pipe for instance

local function getImageForItem(thisItem)
    local itemNumber = tonumber(thisItem)
    local theImage=""

    if itemNumber==3 then
        theImage="hammer.png"
    elseif itemNumber == 7 then
        theImage="metalpipe.png"
    elseif ... -- for other options
        ...
    else
        return nil
    end

    local image = display.newImage(theImage)
    return image
end

local function displayItems()
    local i
    for i=1,#myInventoryBag do
        local x = 0  -- calculate based on the i
        local y = 0 --  calculate based on the i

        local image = getImageForItem(myInventoryBag[i])

        if image==nil then return end

        image.setReferencePoint(display.TopLeftReferencePoint)
        image.x = x
        image.y = y
    end
end

【问题讨论】:

    标签: lua coronasdk moai


    【解决方案1】:
    local itemImages =
    {
        [0] = display.newImage('MISSING_ITEM_IMAGE.PNG'),
        [3] = display.newImage('hammer.png'),
        [7] = display.newImage('metalpipe.png'),
    }
    
    function getImageForItem(itemId)
        return itemImages[itemId] or itemImages[0]
    end
    
    local myInventoryBag={}
    local maxItems = 10 -- change this to how many you want
    local visibleItems = 4 -- show this many items at a time (with arrows to scroll to others)
    
    -- show inventory items at index [first,last]
    local function displayInventoryItems(first,last)
        local x = 0 -- first item goes here
        local y = 0 -- top of inventory row
        for i=first,last do
            image = getImageForItem(myInventoryBag[i])
            image.x = x
            image.y = y
            x = x + image.width
        end
    end
    
    -- show inventory items on a given "page"
    local function displayInventoryPage(page)
        page = page or 1 -- default to showing the first page
        if page > maxItems then
            -- error! handle me!
        end
        local first = (page - 1) * visibleItems + 1
        local last = first + visibleItems - 1
        displayInventoryItems(first, last)
    end
    
    myInventoryBag[5] = 3 -- Hammer for instance
    myInventoryBag[4] = 7 -- A metal Pipe for instance
    
    displayInventoryPage(1)
    displayInventoryPage(2)
    

    【讨论】:

    • 感谢您的帮助,在看了很多次之后,在我看来创建动态库存系统的方法有很多。自从我从事编程7个月以来,仍然学习;有时我倾向于认为,问题必须只有一种方法或解决方案。
    【解决方案2】:

    基本上,您要做的就是遍历所有库存槽并检查槽是否为空。如果它是空的,则将项目放入该插槽并停止循环。如果不是,请转到下一个。

    如果您想从库存中移除物品,只需致电table.delete(myInventoryBag, slotToEmpty)

    对于页面,您只需要一个 page 变量。绘制库存槽时,只需从槽(page-1) * 4 + 1 循环到page * 4

    (编辑:我强烈建议使用适当的缩进,因为它会使代码更具可读性。)

    【讨论】:

    • 我对页面变量和您使用的数字有点困惑。
    • 我认为他的意思是有一个变量来跟踪玩家所在的页面,然后用它来检查所有的插槽。
    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2011-03-15
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多