如何同时使用具有两个变量的变量运行函数

时间:2020-03-11 13:54:25

标签: javascript

我早些时候也问过类似的问题,但是我没有得到答案。我想做的是使用相同的变量运行一个函数两次,该变量对于每个敌人ID都有唯一的值。最终目标是使敌人同时在同一条路径上移动。我想知道的是为什么该功能每次都会重置并且只有一个敌人移动。预先谢谢你!

var lives = 3;
var enemyCountdown = 1000;
var enemyNumber = 1;
var enemyId = "enemy" + enemyNumber;
setText("label1", lives);

function moveAmountnegative (enemyId, moveXAmount, moveYAmount) {
  setPosition(enemyId, (getXPosition(enemyId) - moveXAmount), (getYPosition(enemyId) + moveYAmount));
}
function moveAmountpositive(moveObject, moveXAmount, moveYAmount) {
  setPosition(enemyId, (getXPosition(enemyId) + moveXAmount), (getYPosition(enemyId) - moveYAmount));
}

function moveFunction(enemyNumber) {
  enemyCountdown = "enemyCountdown" + enemyNumber;
  timedLoop(30, function() {
    moveAmountnegative("enemy" + enemyNumber, 0, 1);
    enemyCountdown = enemyCountdown - 1;
    if (enemyCountdown <= countdownIndex[1]) {
      stopTimedLoop();
      timedLoop(30, function() {
        moveAmountpositive("enemy" + enemyNumber, 1, 0);
        enemyCountdown = enemyCountdown - 1;
        if (enemyCountdown <= countdownIndex[2]) {
          stopTimedLoop();
          timedLoop(30, function() {
           moveAmountnegative("enemy" + enemyNumber, 0, 1);
           enemyCountdown = enemyCountdown - 1;
           if (enemyCountdown <= countdownIndex[3]) {
             stopTimedLoop();
             timedLoop(30, function() {
               moveAmountnegative("enemy" + enemyNumber, 1, 0);
               enemyCountdown = enemyCountdown - 1;
               if (enemyCountdown <= countdownIndex[4]) {
                stopTimedLoop();
                lives--;
                setNumber("label1", lives);
                hideElement("enemy" + enemyNumber);
                }
             });
            }
          });
        }
      });
    }
  });
}

onEvent("button1", "click", function() {
  timedLoop(3000, function() {
    moveFunction(1000);
    enemyNumber++;
  });
});

0 个答案:

没有答案