我要实现的是,当IF
语句为true
时,fightID
更改为其他值,因此它与之前分配的值不匹配,因此我无法战斗结束后,不要立即通过控制台运行win-fight
发射。但是我现在只得到Bad id
,所以它甚至都不会运行win-fight
发射。我该如何解决?
var uuid = uuid4();
socket.on('attack', () => {
var fightID = uuid;
if(enemyHealth === 0 && uuid4.valid(uuid) === fightID|| userHealth === 0
&& enemyHealth === 0 && uuid4.valid(uuid) === fightID) {
fightID = 185;
socket.emit('win-fight'); // next emit
} else {
console.log('Bad id');
}
});
答案 0 :(得分:1)
如果我正确理解了您的问题,则应通过以下调整来解决该问题:
var uuid = uuid4();
var isFightFinished = false;
// Declare fightID here
var fightID = uuid;
socket.on('attack', () => {
// Remove fightID from here
// var fightID = uuid;
if(isFightFinished === false && (enemyHealth <= 0 || userHealth <= 0)) {
// Now you're newly assigned fightID is remembered for the next attack
// event
fightID = 185;
isFightFinished = true;
socket.emit('win-fight'); // next emit
} else {
console.log('Bad id');
}
});