我有一个技能计算器,对我来说已经工作了一段时间,但是一直缺少一些我没有Java经验的人无法理解的东西。

我想补充的是,我不希望技能2除非技能1 =最高而增加,否则不希望技能3除非技能2 = 5以上且不提高,除非技能2和3 = max否则不提高。

那有意义吗?我在这里让自己有些困惑。谢谢大家,任何帮助或朝着正确方向发展的观点都将是惊人的。

我的技能计算器:

<script type="text/javascript">
    var SkillManager = (function() {
        var max = 50,
            skills = {
                skill1: {
                    cur: 0,
                    max: 10
                },
                skill2: {
                    cur: 0,
                    max: 10
                },
                skill3: {
                    cur: 0,
                    max: 10
                },
                skill4: {
                    cur: 0,
                    max: 10
                }
            },
            totalUsed = 0;

var increase = function(skill) {
    if (totalUsed < max && skills[skill].cur < skills[skill].max) {
        skills[skill].cur++;
        totalUsed++;
        updateDisplay(skill, skills[skill].cur, max - totalUsed);
    } else if(skills[skill].cur === skills[skill].max) {
        alert("You have maxed out that skill!");
    } else {
        alert("You have used all your skill points!");
    }
};

var decrease = function(skill) {
    if (skills[skill].cur > 0) {
        skills[skill].cur--;
        totalUsed--;
        updateDisplay(skill, skills[skill].cur, max - totalUsed);
    } else {
        alert("You can't decrease a skill with 0 points in it!");
    }
};

var updateDisplay = function(skill, value, totalRemaining) {
if(document.all){
    document.getElementById(skill + "counter").innerText = value;
    document.getElementById("remainingPoints").innerText = totalRemaining;
} else {
    document.getElementById(skill + "counter").textContent = value;
    document.getElementById("remainingPoints").textContent = totalRemaining;
}

};

return {
    decrease: decrease,
    increase: increase
};
}());

</script>

最佳答案

尝试

var increase = function(skill) {
    if(skill == 'skill2' && skills.skill1.cur < skills.skill1.max){
        alert('skill2 -> skill1 < ' + skills.skill1.max)
    } else if (skill == 'skill3' && skills.skill2.cur < 5) {
        alert('skill3 -> skill2 < 5')
    } else if (skill == 'skill4' && (skills.skill2.cur < skills.skill2.max || skills.skill3.cur < skills.skill3.max)) {
        alert('skill2 -> skill2 < ' + skills.skill2.max + ' or skill3 < ' + skills.skill3.max)
    } else if (totalUsed < max && skills[skill].cur < skills[skill].max) {
        skills[skill].cur++;
        totalUsed++;
        updateDisplay(skill, skills[skill].cur, max - totalUsed);
    } else if(skills[skill].cur === skills[skill].max) {
        alert("You have maxed out that skill!");
    } else {
        alert("You have used all your skill points!");
    }
};


演示:Fiddle

09-25 19:08