gpt4 book ai didi

javascript - 停止变量增加,除非另一个变量 = max

转载 作者:行者123 更新时间:2023-11-28 09:08:08 27 4
gpt4 key购买 nike

我有一个技能计算器,它已经对我工作了一段时间了,但总是缺少一些东西,作为一个没有 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>

最佳答案

尝试

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

关于javascript - 停止变量增加,除非另一个变量 = max,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16646930/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com