我正在研究一个文本RPG,我只是搞砸了,测试我是如何做XP系统的。当玩家的XP大于或等于所需的XP时,我试图将其减去,然后减去它们并升级玩家。我无法弄清楚我做错了什么。 (我遇到的问题是PlayerXP
直接超过了所需的XP)
HTML
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script src='script.js'></script>
</head>
<body>
<script>
setInterval(function() {
Player.Stats.XP++;
document.getElementById('PlayerLevel').innerHTML = Player.Stats.Level;
document.getElementById('PlayerXP').innerHTML = Player.Stats.XP
document.getElementById('PlayerNeededXP').innerHTML = Player.Stats.NeededXP;
}, 1000);
</script>
<p>Level: <span id='PlayerLevel'></span></p>
<p>Experience: <span id='PlayerXP'></span> - <span id='PlayerNeededXP'></span></p>
</body>
</html>
的Javascript
NeededEXP = [
/**/,
25, //1
50, //2
100, //3
200, //4
500, //5
1000, //6
1750, //7
3000, //8
5000, //9
10000 //10
];
var Player = new Object();
Player.Stats = new Object();
Player.Stats.Level = 1;
Player.Stats.XP = 0;
Player.Stats.NeededXP = NeededEXP[Player.Stats.Level];
if(Player.Stats.XP >= Player.Stats.NeededXP) {
Player.Stats.XP = Player.Stats.NeededXP - Player.Stats.XP;
Player.Stats.Level++;
}
答案 0 :(得分:0)
您必须在超时的每次迭代中检查当前的XP值:
NeededEXP = [
25, //1
50, //2
100, //3
200, //4
500, //5
1000, //6
1750, //7
3000, //8
5000, //9
10000 //10
];
var Player = {};
Player.Stats = {};
Player.Stats.Level = 1;
Player.Stats.XP = 0;
Player.Stats.NeededXP = NeededEXP[Player.Stats.Level];
setInterval(function() {
Player.Stats.XP++;
if (Player.Stats.XP >= Player.Stats.NeededXP) {
Player.Stats.XP -= Player.Stats.NeededXP;
Player.Stats.Level++;
Player.Stats.NeededXP = NeededEXP[Player.Stats.Level];
}
document.getElementById('PlayerLevel').innerHTML = Player.Stats.Level;
document.getElementById('PlayerXP').innerHTML = Player.Stats.XP
document.getElementById('PlayerNeededXP').innerHTML = Player.Stats.NeededXP;
}, 1000);
<p>Level: <span id='PlayerLevel'></span>
</p>
<p>Experience: <span id='PlayerXP'></span> - <span id='PlayerNeededXP'></span>
</p>
答案 1 :(得分:0)
您可以像这样
在setInterval功能块中剪切和粘贴检查片段setInterval(function() {
Player.Stats.XP++;
if(Player.Stats.XP >= Player.Stats.NeededXP) {
Player.Stats.XP = Player.Stats.NeededXP - Player.Stats.XP;
Player.Stats.Level++;
}
document.getElementById('PlayerLevel').innerHTML = Player.Stats.Level;
document.getElementById('PlayerXP').innerHTML = Player.Stats.XP
document.getElementById('PlayerNeededXP').innerHTML = Player.Stats.NeededXP;
}, 1000);