1000行代码写小游戏(四)

更新金币、钥匙和收藏品:

-- 更新金币
function MineSecretDialog:updateUserMoney(addMoney, isInit)
    addMoney = addMoney or 0
    isInit = isInit or false

    if addMoney ~= 0 or isInit then
        userData.money = math.max(0, userData.money + addMoney)
        AddLabel( "$ "..userData.money, ccp(posX2 + 151, posY - gap), self.m_bgKuang, Tag.MoneyLabel, ColorList[2], ccp(0, 0.5) )
    end
end
-- 更新铜,银,金钥匙
function MineSecretDialog:updateUserKey(addIronKey, addSilverKey, addGoldKey, isInit)
    addIronKey = addIronKey or 0
    addSilverKey = addSilverKey or 0
    addGoldKey = addGoldKey or 0
    isInit = isInit or false

    if addIronKey ~= 0 or isInit then
        userData.ironKey = math.min(BarMaxNum.KEY, math.max(0, userData.ironKey + addIronKey))
        AddLabel( userData.ironKey, ccp(posX2 + 163, posY - gap * 2), self.m_bgKuang, Tag.IronKeyLabel, H_Font_Gray_Stroke_10, ccp(0.5, 0.5) )
    end

    if addSilverKey ~= 0 or isInit then
        userData.silverKey = math.min(BarMaxNum.KEY, math.max(0, userData.silverKey + addSilverKey))
        AddLabel( userData.silverKey, ccp(posX2 + 190, posY - gap * 2), self.m_bgKuang, Tag.SilverKeyLabel, H_Font_Gray_Stroke_10, ccp(0.5, 0.5) )
    end

    if addGoldKey ~= 0 or isInit then
        userData.goldKey = math.min(BarMaxNum.KEY, math.max(0, userData.goldKey + addGoldKey))
        AddLabel( userData.goldKey, ccp(posX2 + 217, posY - gap * 2), self.m_bgKuang, Tag.GoldKeyLabel, H_Font_Gray_Stroke_10, ccp(0.5, 0.5) )
    end
end
-- 更新收藏品
function MineSecretDialog:updateUserCollection(collectLetter, addNum, isInit)
    addNum = addNum or 1
    isInit = isInit or false
    local count = 0

    if collectLetter ~= nil then 
        for i=1,#(userData.collection) do
            if userData.collection[i].letter == collectLetter then 
                userData.collection[i].num = math.min(BarMaxNum.COLLECT, userData.collection[i].num + addNum)
                local color = H_Font_WhiteSmall
                if userData.collection[i].num > 0 then color = ColorList[i] end
                AddLabel( userData.collection[i].letter, ccp(15 + (i-1)*20, posY - gap * 2), self.m_bgKuang, Tag.CollectionLabel+i, color, ccp(0.5, 0.5) )
            end
            if userData.collection[i].num > 0 then count = count + 1 end
        end
        if count == #(userData.collection) then
            -- TODO: 集齐七龙珠,召唤神龙
            for i=1,#(userData.collection) do
                userData.collection[i].num = userData.collection[i].num - 1
            end
            isInit = true
        end
    end

    if isInit then
        for i=1,#(userData.collection) do
            local color = H_Font_WhiteSmall
            if userData.collection[i].num > 0 then color = ColorList[i] end
            AddLabel( userData.collection[i].letter, ccp(15 + (i-1)*20, posY - gap * 2), self.m_bgKuang, Tag.CollectionLabel+i, color, ccp(0.5, 0.5) )
        end
    end
end


你可能感兴趣的:(Lua)