1

我有一个技能计算器,它已经为我工作了一段时间,但一直缺少一些我无法理解的东西,因为我没有 javascript 经验。

我想补充的是,我不希望技能 2 增加,除非技能 1 = 最大值,但技能 3 不增加,除非技能 2 = 高于 5,技能 4 不增加,除非技能 2 和 3 = 最大值。

那有意义吗?我在这里有点困惑。任何帮助或正确方向的观点都会很棒,谢谢大家。

我的技能计算器:

<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>
4

2 回答 2

0

看起来你是 99% 的家。

只需在进行初始比较之前添加另一个开关,查看正在传递的技能,然后根据您描述或想要的任何逻辑,如果不满足条件,只需退出该方法。

所以...

if(skill == 'skill2'){
     if(skills["skill1"].cur < skills["skill1"].max)
         return;

     //etc... whatever other tests you wish to run
 }

 if (totalUsed < max && skills[skill].cur < skills[skill].max) {

//the rest of your code
 }
于 2013-05-20T10:06:35.130 回答
0

尝试

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!");
    }
};

演示:小提琴

于 2013-05-20T10:35:18.427 回答