在发射完成之后而不是在发射之前更改IF语句中的值

时间:2018-10-14 20:58:17

标签: javascript socket.io

我要实现的是,当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');
       }

    });

1 个答案:

答案 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');
   }

});