如何为点-and-click游戏设计一个"动态库存系统"?

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

local myInventoryBag={}
local maxItems = 10 --将其更改为您想要的项目数

myInventoryBag[5]=3 --例如,使用钉槌
myInventoryBag[4]=7 --例如使用金属管

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 ... --其他选项
        ...
    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  --根据i计算
        local y = 0 -- 根据i计算

        local image = getImageForItem(myInventoryBag[i])

        if image==nil then return end

        image.setReferencePoint(display.TopLeftReferencePoint)
        image.x = x
        image.y = y
    end
end
点赞
用户1576117
用户1576117

基本上,您要做的是遍历所有的库存插槽并检查插槽是否为空。如果为空,就将物品放置在那个插槽里并停止循环。如果不是,就转到下一个。

如果您想从库存中删除一个物品,只需调用 table.delete(myInventoryBag,slotToEmpty)

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

(编辑:我强烈建议使用适当的缩进,因为它将使代码更加易读。)

2012-09-27 07:51:04
用户501459
用户501459
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 -- 将此变量改为你希望的值
local visibleItems = 4 -- 同时显示这么多物品(带有箭头用于滚动到其他物品)

-- 在索引[first,last]处显示物品
local function displayInventoryItems(first,last)
    local x = 0 -- 第一个物品的位置
    local y = 0 -- 物品所在行的顶部
    for i=first,last do
        image = getImageForItem(myInventoryBag[i])
        image.x = x
        image.y = y
        x = x + image.width
    end
end

-- 在给定的“页面”上显示物品
local function displayInventoryPage(page)
    page = page or 1 -- 默认显示第一页
    if page > maxItems then
        -- 错误!要处理我!
    end
    local first = (page - 1) * visibleItems + 1
    local last = first + visibleItems - 1
    displayInventoryItems(first, last)
end

myInventoryBag[5] = 3 -- 例如,给第五个位置放入铁锤
myInventoryBag[4] = 7 -- 例如,给第四个位置放入金属管

displayInventoryPage(1)
displayInventoryPage(2)
2012-09-27 21:57:07