1000行代码写小游戏(三)

更新玩家的血量、攻击、防御等属性数值和展示:

-- 更新血量
function MineSecretDialog:updateUserHp(addHp, addMaxHp, isInit)
    addHp = addHp or 0
    addMaxHp = addMaxHp or 0
    isInit = isInit or false

    local updateHp = false
    if addMaxHp > 0 then
        userData.maxHp = userData.maxHp + addMaxHp
        updateHp = true
    end
    if addHp ~= 0 then
        userData.curHp = math.min(userData.maxHp, math.max(0, userData.curHp + addHp))
        updateHp = true
    end

    if updateHp or isInit then
        self.m_hpBar:setScaleX(countBarScaleX(userData.curHp/userData.maxHp, userData.maxHp/BarMaxNum.HP))
        AddLabel( userData.curHp.."/"..userData.maxHp, ccp(posX + 5, posY - gap), self.m_bgKuang, Tag.HpNumLabel, ColorList[2], ccp(0, 0.5) )
    end
end
-- 更新体力
function MineSecretDialog:updateUserActive(addActive, addMaxActive, isInit)
    addActive = addActive or 0
    addMaxActive = addMaxActive or 0
    isInit = isInit or false

    local updateActive = false
    if addMaxActive > 0 then
        userData.maxActive = userData.maxActive + addMaxActive
        updateActive = true
    end
    if addActive ~= 0 then
        userData.curActive = math.min(userData.maxActive, math.max(0, userData.curActive + addActive))
        updateActive = true
    end

    if updateActive or isInit then
        self.m_activeBar:setScaleX(countBarScaleX(userData.curActive/userData.maxActive, userData.maxActive/BarMaxNum.ACTIVE))
        AddLabel( userData.curActive.."/"..userData.maxActive, ccp(posX + 5, posY - gap * 2), self.m_bgKuang, Tag.ActiveNumLabel, ColorList[3], ccp(0, 0.5) )
    end
end
-- 更新恢复,攻击,防御
function MineSecretDialog:addUserRecoverAtkDef(addRecover, addAtk, addDef, isInit)
    addRecover = addRecover or 0
    addAtk = addAtk or 0
    addDef = addDef or 0
    isInit = isInit or false

    if addRecover > 0 or isInit then
        userData.recover = userData.recover + addRecover
        self.m_recoverBar:setScaleX(countBarScaleX(userData.recover/BarMaxNum.RECOVER))
        AddLabel( userData.recover, ccp(posX2 + 5, posY), self.m_bgKuang, Tag.RecoverNumLabel, ColorList[4], ccp(0, 0.5) )
    end

    if addAtk > 0 or isInit then
        userData.atk = userData.atk + addAtk
        self.m_atkBar:setScaleX(countBarScaleX(userData.atk/BarMaxNum.ATK))
        AddLabel( userData.atk, ccp(posX2 + 5, posY - gap), self.m_bgKuang, Tag.AtkNumLabel, ColorList[5], ccp(0, 0.5) )
    end

    if addDef > 0 or isInit then
        userData.def = userData.def + addDef
        self.m_defBar:setScaleX(countBarScaleX(userData.def/BarMaxNum.DEF))
        AddLabel( userData.def, ccp(posX2 + 5, posY - gap * 2), self.m_bgKuang, Tag.DefNumLabel, ColorList[6], ccp(0, 0.5) )
    end
end


你可能感兴趣的:(Lua)