几周前,我开始使用javascript和firebase创建自己的游戏,并且在更新数据时遇到了一些问题。当我为这款游戏创建战斗系统时,我试图从打架中给玩家一些好处。当玩家赢得一场战斗时,我想通过添加exp来更新其(例如)exp,它可以正常工作,但是当我添加另一个更新数据时(当玩家赢得一场战斗时,系统会更新有关战斗胜利的数据以及有关统计信息的数据位置在数据库中的不同位置),则返回循环。 JS代码在这里
// battle_system.js
var opponent = nextBattle.getAttribute('mon-name');
var hp = 0;
var getPlayer = firebase.database().ref().child('accounts/' + uid + '/character/char');
var getEnemy = firebase.database().ref().child('Monsters/' + opponent);
getPlayer.on('value', (snapshot) => {
var getSnapshot = snapshot.val();
getEnemy.on('value', (snap) => {
var getSnap = snap.val();
var enemyHp = getSnap.hp;
var playerHp = getSnapshot.hp;
var enemy = {
name: getSnap.name,
hp: enemyHp,
damage: getSnap.dmg,
armor: getSnap.armor,
lvl: getSnap.lvl
}
var player = {
name: getSnapshot.nick,
hp: playerHp,
damage: getSnapshot.dmg,
armor: getSnapshot.armor,
lvl: getSnapshot.lvl
}
var starCountRef = firebase.database().ref('accounts/' + uid + '/character/battles/stats/' + opponent);
btl_section.innerHTML = '';
starCountRef.on('value', (snapStat) => {
var x = (snapStat.val().wins)+1;
var y = (snapStat.val().loses)+1;
while(player.hp > 0 && enemy.hp > 0){
var MathPlayer = Math.random(),
MathEnemy = Math.random();
enemy.hp -= player.damage * MathPlayer;
player.hp -= enemy.damage * MathEnemy;
btl_section.innerHTML = btl_section.innerHTML + 'Enemy: ' + enemy.hp + '<br> Player: ' + player.hp + '<br>';
if(player.hp < 0){
firebase.database().ref('accounts/' + uid + '/character/battles/stats/' + opponent).update({
loses : y
});
}else if(enemy.hp < 0) {
console.log('win');
firebase.database().ref('accounts/' + uid + '/character/battles/stats/' + opponent).update({
wins : x
});
firebase.database().ref('accounts/' + uid + '/character/char').update({
exp : (30 * (Math.random()*100))
});
}
}
});
});
});
//firebase structure
Monsters: {
Freder : {
armor: 0,
dmg: 10,
lvl: 1,
hp: 100,
name: 'Freder'
}
},
accounts:{
uid:{
character:{
battles:{
stats: {
wins: 0,
loses: 0
}
},
char:{
exp: 0,
lvl: 0,
dmg: 12,
hp: 100,
armor: 0,
nick: 'Nickname'
}
}
}
}
现在,除了玩家赢得战斗时,我将毫无疑问地添加有关胜利的经验值和统计信息,但战斗仍在进行中。当我在浏览器控制台中看到关于获胜游戏的consol.log时,我从结果中看到数百条日志,几秒钟后,游戏返回错误“未捕获的RangeError:超出最大调用堆栈大小”。现在我需要有关此的帮助。我该如何解决?